Commit d43f0d9b authored by Alec Dayupay's avatar Alec Dayupay

Added detail/create/update view for Forum. Added templates/base.html and...

Added detail/create/update view for Forum. Added templates/base.html and forum/forms.py. Modified forum/models.py.
parent afd328ee
from django.forms import ModelForm
from .models import ForumPost
class ForumPostForm(ModelForm):
class Meta:
model = ForumPost
fields = '__all__'
\ No newline at end of file
# Generated by Django 3.2 on 2023-04-28 05:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_reply_forum_post'),
]
operations = [
migrations.AlterField(
model_name='forumpost',
name='pub_datetime',
field=models.DateTimeField(auto_now_add=True),
),
]
from django.db import models
from django.urls import reverse
class ForumPost(models.Model):
title = models.CharField(max_length = 50)
body = models.TextField()
author = models.ForeignKey('dashboard.WidgetUser', on_delete=models.CASCADE)
pub_datetime = models.DateTimeField()
pub_datetime = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
def datetime(self):
return self.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
def get_absolute_url(self):
return reverse("forumpost-details", kwargs={'pk': self.pk})
class Reply(models.Model):
body = models.TextField()
author = models.ForeignKey('dashboard.WidgetUser', on_delete=models.CASCADE)
......@@ -17,3 +24,6 @@ class Reply(models.Model):
def __str__(self):
return self.body
def datetime(self):
return self.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Forum{% endblock %}
{% block heading %}Welcome to Widget's Forum!{% endblock %}
{% block content %}
<p>Forum posts:</p>
{% for post in forumposts %}
<p><a href="{% url 'forumpost-details' post.pk %}">{{ post.title }} by {{ post.author }}</a></p>
{% endfor %}
<input type="button" value="New Post" onclick="location.href='{% url 'forumpost-add' %}'"/>
{% endblock %}
{% block links %}
<p><a href="http://localhost:8000/dashboard/">Dashboard</a></p>
<p><a href="http://localhost:8000/announcements/">Announcement</a></p>
<p><a href="http://localhost:8000/assignments/">Assignment</a></p>
<p><a href="http://localhost:8000/calendar/">Calendar</a></p>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Post{% endblock %}
{% block content %}
<p>Add a new post:</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Save New Post">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.title }}{% endblock %}
{% block heading %}{{ object.title }}{% endblock %}
{% block content %}
<p>by {{ object.author }}</p>
<p>{{ object.datetime }}</p>
<p>{{ object.body }}</p>
<p><br>POST REPLIES:</p>
{% for reply in object.reply_set.all %}
<p>by {{ reply.author }}</p>
<p>{{ reply.datetime }}</p>
<p>{{ reply.body }}<br></p>
{% endfor %}
<input type="button" value="Edit Post" onclick="location.href='{% url 'forumpost-edit' object.pk %}'"/>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Post{% endblock %}
{% block content %}
<p>Edit Post:</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Save Changes to Post">
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path
from . import views
from .views import (ForumPostDetailView, ForumPostCreateView, ForumPostUpdateView)
urlpatterns = [
path('', views.forum, name="forum")
path('', views.forum, name="forum"),
path('forumposts/<int:pk>/details/', ForumPostDetailView.as_view(), name="forumpost-details"),
path('forumposts/add/', ForumPostCreateView.as_view(), name="forumpost-add"),
path('forumposts/<int:pk>/edit/', ForumPostUpdateView.as_view(), name="forumpost-edit"),
]
\ No newline at end of file
from django.http import HttpResponse
from .models import ForumPost, Reply
from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import ForumPost
def forum(request):
forumposts = ForumPost.objects.all()
replies = Reply.objects.all()
return render(request, 'forum/forum.html', {'forumposts': ForumPost.objects.all()})
response = "Widget's Forum<br><br>Forum Posts:<br>"
for post in forumposts:
response += "{} by {} posted {}:<br>{}<br>".format(
post.title,
post.author,
post.pub_datetime.strftime("%m/%d/%Y, %I:%M %p"),
post.body
)
for reply in replies:
if reply.forum_post == post:
response += "Reply by {} posted {}:<br>{}<br>".format(
reply.author,
reply.pub_datetime.strftime("%m/%d/%Y, %I:%M %p"),
reply.body
)
response += "<br>"
class ForumPostDetailView(DetailView):
model = ForumPost
template_name = 'forum/forumpost-details.html'
return HttpResponse(response)
class ForumPostCreateView(CreateView):
model = ForumPost
fields = '__all__'
template_name = 'forum/forumpost-add.html'
class ForumPostUpdateView(UpdateView):
model = ForumPost
fields = '__all__'
template_name = 'forum/forumpost-edit.html'
\ No newline at end of file
<html>
<head>
<title>{% block title%}{% endblock %}</title>
</head>
<body>
<div id="heading">
<h1>{% block heading %}{% endblock %}</h1>
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
<div id="links">
{% block links %}{% endblock %}
</div>
</body>
</html>
\ No newline at end of file
......@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......@@ -59,7 +60,7 @@ ROOT_URLCONF = 'widget_bigdjangoenergy.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......
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