Commit 98c380aa authored by Jersey Dayao's avatar Jersey Dayao 🏀

Merge branch 'dayao/forum'

parents f96c5861 b6ac8a51
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,17 +6,27 @@ from homepage.models import WidgetUser ...@@ -6,17 +6,27 @@ from homepage.models import WidgetUser
class Post(models.Model): class Post(models.Model):
post_title = models.CharField(max_length=100) post_title = models.CharField(max_length=100)
post_body = models.CharField(max_length=10000) post_body = models.CharField(max_length=10000)
pub_date = models.DateTimeField('date published') pub_date = models.DateTimeField('date published', auto_now=True)
author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE, default=None, null=True) author = models.ForeignKey('homepage.WidgetUser',
on_delete=models.CASCADE,
default=None,
null=True)
def __str__(self): def __str__(self):
return self.post_title return self.post_title
class Reply(models.Model): class Reply(models.Model):
reply_body = models.CharField(max_length=10000) reply_body = models.CharField(max_length=10000)
pub_date = models.DateTimeField('date published') pub_date = models.DateTimeField('date published')
reply_post = models.ForeignKey(Post, related_name="replies", on_delete=models.CASCADE, default=None, null=True) reply_post = models.ForeignKey(Post,
author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE, default=None, null=True) 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): def __str__(self):
return 'Reply to: {}' .format(self.reply_post) return 'Reply to: {}' .format(self.reply_post)
\ No newline at end of file
...@@ -9,6 +9,11 @@ h1 { ...@@ -9,6 +9,11 @@ h1 {
h2, li { h2, li {
font-family: Garamond, serif; font-family: Garamond, serif;
color: #3E2723; color: #3E2723;
}
#createHeader {
padding-top: 45px;
} }
h3 { 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 @@ ...@@ -2,18 +2,33 @@
{% block content %} {% block content %}
<h1>Welcome to Widget's Forum! :)</h1>
<h1>Welcome to Widget's Forum! :)</h1>
<br/> <br/>
<h2>Forum Posts:</h2> <h2>Forum Posts:</h2>
{% if post_list %} {% if post_list %}
<u1> <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> <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 %} {% endfor %}
</u1> </u1>
{% else %} {% else %}
<p>No posts are available.</p> <p>No posts are available.</p>
{% endif %} {% 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 %} {% endblock %}
\ No newline at end of file
...@@ -3,7 +3,8 @@ from . import views ...@@ -3,7 +3,8 @@ from . import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="Forum"), 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" app_name = "forum"
\ No newline at end of file
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
from django.shortcuts import render from django.shortcuts import render, redirect
from .models import Post from .models import Post
from .forms import PostForm
# Create your views here. # Create your views here.
...@@ -12,6 +13,19 @@ def index(request): ...@@ -12,6 +13,19 @@ def index(request):
} }
return render(request, "forum/index.html", context) 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 # posts/<post_id>/details
def details(request, post_id): def details(request, post_id):
try: try:
......
...@@ -134,8 +134,4 @@ STATIC_URL = '/static/' ...@@ -134,8 +134,4 @@ STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/' MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '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