Commit 0347c901 authored by Ryan Angelo G. Lim's avatar Ryan Angelo G. Lim

Added feature on forum app that allows a user to create a new post.

parent cec79492
from django import forms
from django.forms import ModelForm
from .models import *
class PostForm(ModelForm):
class Meta:
model = Post
fields ='__all__'
# Generated by Django 4.0.3 on 2022-05-24 10:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0007_alter_post_pub_date_alter_reply_reply_pub_date'),
]
operations = [
migrations.AddField(
model_name='post',
name='post_img',
field=models.ImageField(blank=True, null=True, upload_to=''),
),
]
......@@ -7,6 +7,7 @@ class Post(models.Model):
post_body = models.CharField(max_length=500)
pub_date = models.DateField("date published", auto_now_add=True)
post_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='post_author')
post_img = models.ImageField(upload_to="forumimg/", height_field=None, width_field=None, max_length=100,null=True, blank=True)
def __str__(self):
return self.post_title
......
{% extends "forum/base.html" %}
{% block content %}
<div class="RectangularShadow"></div>
<h1 style="text-align: center"> New Post </h1>
<form action="{% url 'forum:add' %}" method ="POST" enctype="multipart/form-data">
{% csrf_token %}
{{post_form.as_p}}
<button class="button" type="submit">Save Post</button>
</form>
<a href="/forum/"> <button>Back to Forums</button> </a>
{% endblock %}
{% extends "forum/base.html" %}
{% block content %}
{% if Post %}
<img src="/static/forum/{{Post.post_title}}.jpg" class="detailsRelatedPicture">
<img src="/static/forum/{{Post.post_author.last_name}}.jpg" class="detailsProfilePicture">
<img src="{{ Post.post_img.url }}" class="detailsRelatedPicture">
<img src="{{ Post.post_author.picture.url }}" class="detailsProfilePicture">
<div class="RectangularShadow"></div>
<h1 style="text-align: center">{{Post.post_title}}</h1>
<h2>by {{Post.post_author.first_name}} {{Post.post_author.last_name}}, {{Post.pub_date}}</h2>
......
......@@ -12,4 +12,5 @@
{% else %}
<p>No Posts are available</p>
{% endif %}
<a href="posts/add"> <button>Create a Post</button> </a>
{% endblock %}
......@@ -2,7 +2,9 @@ from django.urls import path
from . import views
app_name="forum"
urlpatterns = [
path('', views.index, name="forum"),
path('posts/add/', views.add, name="add"),
path('posts/<int:post_id>/details/', views.details, name="details"),
]
from array import array
from django.http import HttpResponse, Http404
from forum.models import Post, Reply, WidgetUser
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.template import loader
from .forms import PostForm
# Create your views here.
def index(request):
......@@ -12,45 +13,12 @@ def index(request):
}
return render(request, "forum/index.html", context)
#entries = Post.objects.all().count()
#n = 1
#forum = "FORUM POSTS:<br/>"
#while(n<=entries):
# forum += getPost(n)
# forum += getReply(n)
# n += 1
#return HttpResponse(forum)
#def getPost(pk):
# postFirstLine = ""
# postSecondLine = ""
# post = ""
# postTitle = Post.objects.get(pk=pk).post_title
# authorFirstName = Post.objects.get(pk=pk).post_author.first_name
# authorLastName = Post.objects.get(pk=pk).post_author.last_name
# publicationDate = Post.objects.get(pk=pk).pub_date
# postBody = Post.objects.get(pk=pk).post_body
# postFirstLine = "{} by {} {} dated {}:<br/>" .format(postTitle, authorFirstName, authorLastName, publicationDate)
# postSecondLine = "{}<br/>" .format(postBody)
# post += postFirstLine + postSecondLine
# return post
def getReply(pk):
postReply = []
for reply in Post.objects.get(pk=pk).posts.all():
postReply.append(reply)
return postReply
#replies = ""
#for reply in Post.objects.get(pk=pk).posts.all():
# replies += "Reply by {} {} {}:<br/>{}" .format(reply.reply_author.first_name, reply.reply_author.last_name, reply.reply_pub_date, reply.reply_body)
# replies += "<br/>"
#replies += "<br/>"
#return replies
def details(request, post_id):
try:
post = Post.objects.get(pk=post_id)
......@@ -65,3 +33,13 @@ def details(request, post_id):
"reply": reply
}
return render(request, "forum/details.html", context)
def add(request):
if request.method == "POST":
post_form = PostForm(request.POST, request.FILES)
if post_form.is_valid():
post_form.save()
return redirect("forum:add")
else:
post_form = PostForm()
return render(request, "forum/add.html", {"post_form": post_form})
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