Commit d45ff09c authored by N1o9r4b7e9r6to's avatar N1o9r4b7e9r6to

found errors with earlier version, with Posting and the image. this should have fixed it

parent e08fe1f5
from django import forms from django import forms
from .models import Post from .models import Post
import os
from django.db import models
"""
def path_and_rename(instance,filename):
upload_to="forum\\static\\images"
ext='png'
if instance.pk:
filename='{}.{}'.format(instance.pk,ext)
#print(os.path.join(upload_to,filename))
return os.path.join(upload_to,filename)
"""
class addPostsForm(forms.ModelForm): class addPostsForm(forms.ModelForm):
post_title = forms.CharField(label='Title:') post_title = forms.CharField(label='Title:')
post_body = forms.CharField(label="Post",widget=forms.Textarea(attrs={'rows':'20','cols':'150'})) post_body = forms.CharField(label="Post",widget=forms.Textarea(attrs={'rows':'20','cols':'150'}))
#photo=models.ImageField(upload_to=path_and_rename,null=True,blank=True)
class Meta: class Meta:
model = Post model = Post
fields = '__all__' fields = '__all__'
# Generated by Django 4.0.3 on 2022-05-21 13:29
from django.db import migrations, models
import forum.models
class Migration(migrations.Migration):
dependencies = [
('forum', '0008_post_photo'),
]
operations = [
migrations.AlterField(
model_name='post',
name='photo',
field=models.ImageField(blank=True, null=True, upload_to=forum.models.path_and_rename),
),
]
...@@ -2,13 +2,20 @@ from django.db import models ...@@ -2,13 +2,20 @@ from django.db import models
from homepage.models import WidgetUser from homepage.models import WidgetUser
import os import os
import datetime import datetime
#from .models import Post
def path_and_rename(instance,filename): def path_and_rename(instance,filename):
#all_posts = models.Post.objects.order_by("-id")
upload_to="forum\\static\\images" upload_to="forum\\static\\images"
ext='png'
if instance.pk: if instance.pk:
ext='png'
#instance = all_posts[0]
filename='{}.{}'.format(instance.pk,ext) filename='{}.{}'.format(instance.pk,ext)
#print(os.path.join(upload_to,filename)) #print(os.path.join(upload_to,filename))
else:
print("------")
return os.path.join(upload_to,filename) return os.path.join(upload_to,filename)
#class date(models.Model): #class date(models.Model):
...@@ -20,7 +27,7 @@ class Post(models.Model): ...@@ -20,7 +27,7 @@ class Post(models.Model):
pub_date = models.DateField(auto_now_add=True) pub_date = models.DateField(auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default = None) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default = None)
post_body = models.CharField(max_length=100000) post_body = models.CharField(max_length=100000)
photo=models.ImageField(upload_to=path_and_rename,null=True,blank=True, default=None) photo=models.ImageField(upload_to=path_and_rename,null=True,blank=True)
def __str__(self): def __str__(self):
return'{}: {}'.format(self.post_title,self.pub_date) return'{}: {}'.format(self.post_title,self.pub_date)
...@@ -35,3 +42,4 @@ class Reply(models.Model): ...@@ -35,3 +42,4 @@ class Reply(models.Model):
return 'Reply to ({} | {}) by {} - {}... | {}'.format(self.post.post_title,self.post.pub_date, self.author, self.reply_body[0:50],self.pub_date) return 'Reply to ({} | {}) by {} - {}... | {}'.format(self.post.post_title,self.post.pub_date, self.author, self.reply_body[0:50],self.pub_date)
else: else:
return 'Reply to ({} | {}) by {} - {} | {}'.format(self.post.post_title,self.post.pub_date, self.author, self.reply_body,self.pub_date) return 'Reply to ({} | {}) by {} - {} | {}'.format(self.post.post_title,self.post.pub_date, self.author, self.reply_body,self.pub_date)
This diff is collapsed.
This diff is collapsed.
...@@ -3,18 +3,20 @@ ...@@ -3,18 +3,20 @@
{% block content %} {% block content %}
<h1><center>New Forum Post</center></h1> <h1><center>New Forum Post</center></h1>
<a href = "/forum/" style="background-color:whitesmoke;border:2px solid black;">Back to main Forum</a> <a href = "/forum/" style="background-color:whitesmoke;border:2px solid black;">Back to main Forum</a>
<button class="button" type="submit">Save Post</button>
<form action="{% url 'forum:addForumPosts' %}" method="POST"> <form action="{% url 'forum:addForumPosts' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
<center>Title:{{addposts.post_title}} <center>Title:{{addposts.post_title}}
<br> <br>
Author: {{addposts.author}} {{addposts.pub_date}}</center> Author: {{addposts.author}} {{addposts.pub_date}}</center>
Optional: {{addposts.photo}}
<br> <br>
<p6 style="background-color:aquamarine;">Post Body:</p6> <p6 style="background-color:aquamarine;">Post Body:</p6>
<center style="background-color: aquamarine;">{{addposts.post_body}}</center> <center style="background-color: aquamarine;">{{addposts.post_body}}</center>
<br> <br>
Optional: Add Photo <button class="button" type="submit">Save Post</button>
{{addposts.photo}}
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -4,15 +4,16 @@ ...@@ -4,15 +4,16 @@
<center><h1>This is Widget's Forum!</h1></center> <center><h1>This is Widget's Forum!</h1></center>
<h3>Forum Posts:</h3> <h3>Forum Posts:</h3>
<form action="/posts/add/">
<input type="submit" value="New Forum Post"/>
</form>
<h4> <h4>
{% for posts in all_posts %} {% for posts in all_posts %}
<ul><li><a href="/posts/{{ posts.id }}/details">'{{posts.post_title}}'</a> by <i>{{posts.author.first_name}} {{posts.author.last_name}}</i> <ul><li><a href="/posts/{{ posts.id }}/details">'{{posts.post_title}}'</a> by <i>{{posts.author.first_name}} {{posts.author.last_name}}</i>
dated {{posts.pub_date|date:'d/m/Y'}}</li></ul> dated {{posts.pub_date|date:'d/m/Y'}}</li></ul>
{% endfor %} {% endfor %}
</h4> </h4>
<form action="/posts/add/">
<input type="submit" value="New Forum Post"/>
</form>
<left><img src="../../static/images/twopeopletalking.jpg" alt="people talking" style="width:450px;float:left;"></left> <left><img src="../../static/images/twopeopletalking.jpg" alt="people talking" style="width:450px;float:left;"></left>
<center><img src="../../static/images/forum.PNG" align="left" alt="people bubbes" style="width:450px;;margin-left:20px;"></center> <center><img src="../../static/images/forum.PNG" align="left" alt="people bubbes" style="width:450px;;margin-left:20px;"></center>
<right><img src="../../static/images/teachingstudents.jpg" alt="teacher teaching" style="width:450px;float:right;"> <right><img src="../../static/images/teachingstudents.jpg" alt="teacher teaching" style="width:450px;float:right;">
......
...@@ -9,6 +9,7 @@ from .forms import addPostsForm ...@@ -9,6 +9,7 @@ from .forms import addPostsForm
from django.views import View from django.views import View
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.core.files.storage import FileSystemStorage
class forumposts(View): class forumposts(View):
def get(self, request): def get(self, request):
...@@ -33,12 +34,32 @@ def DisplayPostdetails(request, post_id): ...@@ -33,12 +34,32 @@ def DisplayPostdetails(request, post_id):
return render(request, "forum/details.html", context) return render(request, "forum/details.html", context)
def addForumPosts(request): def addForumPosts(request):
post = Post.objects.order_by("-id")
if request.method == 'POST': if request.method == 'POST':
if request.FILES:
file = request.FILES['photo']
#print("on creation id:",post[0].id+1)
file.name = '{}.{}'.format((post[0].pk)+1,'png')
#print(file.name)
form = addPostsForm(request.POST,(request.FILES))
if not(request.FILES):
form = addPostsForm(request.POST) form = addPostsForm(request.POST)
if form.is_valid(): if form.is_valid():
new_post = form.save() form.save()
return redirect("forum:addForumPosts") return redirect("forum:addForumPosts")
else: else:
form = addPostsForm() form = addPostsForm()
return render(request, "forum/addforumposts.html",{"addposts":form}) return render(request, "forum/addforumposts.html",{"addposts":form})
#NOTE: WHAT IS HAPPENING
#Because there are two request objects, Post which contain the post details,
#and files which typically contain the additional image,
#when it goes to model.py to do changing of extension and path, it will not
#carry request.POST with request.FILES, leaving files with an instance of NONE
#making it not work.
#instead what i did is in this section, in forumposts, I will name the image according to
#the id of the last recorded object.
#LIMITATION: If the recent object is deleted, django's built-in ID system will continue since it
#is not editable. The image however will still be based on the last known object.
#SOLUTION: make temp object. do not delete temp object immediately. create actual object. then may delete temp.
#I have kept the changing in model.py, in case of admin page inputs.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment