Commit b6ac8a51 authored by Jersey Dayao's avatar Jersey Dayao 🏀

feat: added forms (that capture form submission time) to create new posts

parent 82b996f8
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = ["post_title", "post_body", "author"]
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-05-26 19:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0003_alter_reply_reply_post'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(null=True, verbose_name='date published'),
),
]
# Generated by Django 4.0.3 on 2022-05-26 20:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0004_alter_post_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(auto_now=True, verbose_name='date published'),
),
]
......@@ -6,8 +6,11 @@ from homepage.models import WidgetUser
class Post(models.Model):
post_title = models.CharField(max_length=100)
post_body = models.CharField(max_length=10000)
pub_date = models.DateTimeField('date published')
author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE, default=None, null=True)
pub_date = models.DateTimeField('date published', auto_now=True)
author = models.ForeignKey('homepage.WidgetUser',
on_delete=models.CASCADE,
default=None,
null=True)
def __str__(self):
return self.post_title
......@@ -15,8 +18,15 @@ class Post(models.Model):
class Reply(models.Model):
reply_body = models.CharField(max_length=10000)
pub_date = models.DateTimeField('date published')
reply_post = models.ForeignKey(Post, related_name="replies", on_delete=models.CASCADE, default=None, null=True)
author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE, default=None, null=True)
reply_post = models.ForeignKey(Post,
related_name="replies",
on_delete=models.CASCADE,
default=None,
null=True)
author = models.ForeignKey('homepage.WidgetUser',
on_delete=models.CASCADE,
default=None,
null=True)
def __str__(self):
return 'Reply to: {}' .format(self.reply_post)
\ No newline at end of file
......@@ -9,6 +9,11 @@ h1 {
h2, li {
font-family: Garamond, serif;
color: #3E2723;
}
#createHeader {
padding-top: 45px;
}
h3 {
......
{% extends "forum/base.html" %}
{% block content %}
<h2 id="createHeader">New Forum Post</h2>
<form method="POST" action="">
{% csrf_token %}
{{ post_form.as_p }}
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<button class="button" type="submit">Save New Post</button>
</form>
{% endblock %}
\ No newline at end of file
......@@ -2,18 +2,33 @@
{% block content %}
<h1>Welcome to Widget's Forum! :)</h1>
<h1>Welcome to Widget's Forum! :)</h1>
<br/>
<h2>Forum Posts:</h2>
{% if post_list %}
<u1>
{% for post in post_list %}
{% for post in post_list reversed %}
<ol><a href="/forum/{{ post.id }}/details/">{{ post.post_title }} by {{ post.author.first_name }} {{ post.author.last_name }} dated {{ post.pub_date }}</a></ol>
{% endfor %}
</u1>
{% else %}
<p>No posts are available.</p>
{% endif %}
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<!-- pls come back here to link it to posts/add/ -->
<a href="{% url 'forum:create' %}">
<button type="button">New Forum Post</button>
</a>
{% endblock %}
\ No newline at end of file
......@@ -3,7 +3,8 @@ from . import views
urlpatterns = [
path("", views.index, name="Forum"),
path("<int:post_id>/details/", views.details, name="details")
path("add/", views.create, name="create"),
path("<int:post_id>/details/", views.details, name="details"),
]
app_name = "forum"
\ No newline at end of file
from django.http import Http404, HttpResponse
from django.shortcuts import render
from django.shortcuts import render, redirect
from .models import Post
from .forms import PostForm
# Create your views here.
......@@ -12,6 +13,19 @@ def index(request):
}
return render(request, "forum/index.html", context)
def create(request):
if request.method == "POST":
post_form = PostForm(request.POST, request.FILES)
if post_form.is_valid():
post_form.save()
return redirect("forum:Forum")
else:
post_form = PostForm()
return render(request, "forum/create.html", {"post_form": post_form})
# posts/<post_id>/details
def details(request, post_id):
try:
......
......@@ -135,7 +135,3 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
\ No newline at end of file
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
\ 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