Commit 248943fc authored by Chino Tesoro's avatar Chino Tesoro

added feature for adding posts

parent 8781eaa5
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = ["post_title", "post_body", "author", "images"]
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-05-22 20:14
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0006_remove_reply_images_post_images'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(default=datetime.datetime.now, verbose_name='date published'),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateTimeField(default=datetime.datetime.now, verbose_name='date published'),
),
]
from django.db import models
from datetime import datetime
from homepage.models import WidgetUser
class Post (models.Model):
post_title = models.CharField(max_length=99999)
post_body = models.CharField(max_length=99999)
pub_date = models.DateTimeField("date published")
pub_date = models.DateTimeField("date published", default = datetime.now)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
images = models.ImageField (upload_to="forum", default = "default.jpg")
def __str__(self):
......@@ -14,7 +15,7 @@ class Post (models.Model):
class Reply (models.Model):
reply_body = models.CharField(max_length=99999)
pub_date = models.DateTimeField("date published")
pub_date = models.DateTimeField("date published", default = datetime.now)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
def __str__(self):
return f"{self.reply_body} {self.pub_date}"
{% extends 'forum/base.html' %}
{% block title %}New Forum Post{% endblock %}
{% block content %}
<h1>New Forum Post</h1>
<form method="POST" action="{% url 'forum:add' %}"
enctype="multipart/form-data">
{% csrf_token %}
{{ postForm.as_p }}
<button class="button" type="submit">Save Post</button>
<button onclick="window.location.href='../..';">Back to Forum</button>
</form>
{% endblock %}
\ No newline at end of file
......@@ -16,6 +16,7 @@
<p>No replies are available.</p>
{% endif %}
<br /><br /><br /><img src="{{ post.images.url }}" alt="{{post.post_title}}" style="width:50%">
<button onclick="window.location.href='../../..';">Back to Forum</button>
{% endblock %}
......
......@@ -16,4 +16,5 @@
{% else %}
<p>No posts are available.</p>
{% endif %}
<button onclick="window.location.href='posts/add/';">Add Post</button>
{% endblock %}
\ No newline at end of file
......@@ -4,6 +4,8 @@ from . import views
urlpatterns = [
path('', views.index, name='index'),
path("posts/<int:post_id>/details/", views.details, name = "details"),
path("posts/add/", views.add, name="add"),
]
app_name = "forum"
\ No newline at end of file
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.shortcuts import render, redirect
from .models import Post, Reply
posts = Post.objects.order_by("pub_date")
replies = Reply.objects.order_by("pub_date")
from .forms import PostForm
def index (request):
posts = Post.objects.order_by("pub_date")
context = {
"posts": posts
}
......@@ -15,6 +15,9 @@ def index (request):
def details (request, post_id):
posts = Post.objects.order_by("pub_date")
replies = Reply.objects.order_by("pub_date")
try:
post = Post.objects.get (pk=post_id)
except Post.DoesNotExist:
......@@ -30,6 +33,17 @@ def details (request, post_id):
return render(request, "forum/details.html", context)
def add(request):
if request.method == "POST":
postForm = PostForm(request.POST, request.FILES)
if postForm.is_valid():
new_user = postForm.save()
return redirect("forum:index")
else:
postForm = PostForm()
return render(request, "forum/add.html",
{"postForm": postForm})
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