Commit c034e6a2 authored by Deokhyun Lee's avatar Deokhyun Lee

Merge remote-tracking branch 'origin/forumv2'

parents 6c40e867 f194822d
from django import forms
from .models import ForumPost
class AddForumPostForm(forms.ModelForm):
class Meta:
model = ForumPost
fields = '__all__'
# template to be used by default is called ForumPost_form.html
\ No newline at end of file
from django.db import models from django.db import models
from dashboard.models import WidgetUser from dashboard.models import WidgetUser
from django.urls import reverse
# Forum Post # Forum Post
# title; body; author; pub_datetime; # title; body; author; pub_datetime;
...@@ -12,6 +13,9 @@ class ForumPost(models.Model): ...@@ -12,6 +13,9 @@ class ForumPost(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse('forum:forumpost_details', kwargs={'pk':self.pk})
# Reply # Reply
# body; author; pub_datetime; forum_post; # body; author; pub_datetime; forum_post;
class Reply(models.Model): class Reply(models.Model):
......
{% extends 'base.html' %}
{% load static %}
{% block title %} Widget's Forum {% endblock %}
{% block content %}
<h1>Welcome to Widget's Forum!</h1>
<body>
Forum posts:
{% for post in forumposts %}
<li>
<a href="{{ post.get_absolute_url }}">
{{ post.title }} by {{ post.author.last_name }}, {{ post.author.first_name }}
</a>
</li>
{% endfor %}
</body> <br>
<form action="forumposts/add">
<button type="submit">New Post</button>
</form> <br>
<li><a href="/dashboard">Dashboard</a> <br></li>
<li><a href="/announcements">Announcements</a> <br></li>
<li><a href="/assignments">Assignments</a> <br></li>
<li><a href="/calendar">Calendar</a> <br></li>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Add Post {% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p> {{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<p> Add a new post: </p>
<form method = "post">
{% csrf_token %}
{% for field in form %}
{{field.label}}: {{field}}<br><br>
{% endfor %}
&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Save New Post">
</form>
{% endblock %}
{% extends 'base.html'%}
{% block title %}
{{ object.title }}
{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<body>
by {{ object.author.first_name }} {{ object.author.last_name }}<br>
{{ object.pub_datetime|date:"m/d/Y, h:i A" }}<br>
{{ object.body }}<br><br>
POST REPLIES: <br>
{% for reply in replies %}
{% if reply.forum_post == object %}
by {{ reply.author.first_name }} {{ reply.author.last_name }}<br>
{{ reply.pub_datetime|date:"m/d/Y, h:i A" }}<br>
{{ reply.body }}<br><br>
{% endif %}
{% endfor %}
</body>
<form action="./edit">
<button type="submit">Edit Post</button>
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Post {% endblock %}
{% block content %}
<h1>Edit Post:</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save Changes to Post</button>
</form>
{% endblock %}
...@@ -3,5 +3,10 @@ from . import views ...@@ -3,5 +3,10 @@ from . import views
# url for forum # url for forum
urlpatterns = [ urlpatterns = [
path("", views.forumIndex, name="forumIndex"), path('', views.forum_view, name="forumposts"),
path('forumposts/add', views.ForumPostCreateView.as_view(), name="forumpost_add" ),
path('forumposts/<int:pk>/details', views.ForumPostDetailView.as_view(), name='forumpost_details'),
path('forumposts/<int:pk>/edit', views.ForumPostUpdateView.as_view(), name='forumpost_edit'),
] ]
app_name = "forum"
\ No newline at end of file
from django.http import HttpResponse from django.shortcuts import render, redirect
from django.views.generic import DetailView, CreateView, UpdateView, ListView
from .models import ForumPost, Reply from .models import ForumPost, Reply
# forum view from .models # dashboard view from .models
def forumIndex(request): def forum_view(request):
forumposts = ForumPost.objects.all()
# fetch forum posts and replies
forum = ForumPost.objects.all()
replies = Reply.objects.all() replies = Reply.objects.all()
forum_output = "Widget's Forum <br><br> Forum Posts:<br>" return render(request, 'forum.html', {'forumposts': forumposts, 'replies': replies})
for post in forum:
forum_output = (forum_output + post.title + " by " + post.author.first_name + " " + post.author.last_name + class ForumPostDetailView(DetailView):
" posted " + post.pub_datetime.strftime('%m/%d/%Y, %I:%M %p') + ":<br>" + def get_context_data(self, **kwargs):
post.body + "<br>" context = super().get_context_data(**kwargs)
) context['replies'] = Reply.objects.all()
for reply in replies: return context
if reply.forum_post.title == post.title:
forum_output = (forum_output + "Reply by " + reply.author.first_name + " " + reply.author.last_name model = ForumPost
+ " posted " + reply.pub_datetime.strftime('%m/%d/%Y, %I:%M %p') + ":<br>" + template_name = "forumpost-details.html"
reply.body + "<br>"
) class ForumPostCreateView(CreateView):
model = ForumPost
forum_output = forum_output + "<br>" template_name = "forumpost-add.html"
fields = "__all__"
return HttpResponse(forum_output)
class ForumPostUpdateView(UpdateView):
model = ForumPost
template_name = "forumpost-edit.html"
fields = "__all__"
...@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ ...@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
from pathlib import Path from pathlib import Path
import os import os
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
...@@ -22,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent ...@@ -22,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
from decouple import config from decouple import config
SECRET_KEY = config("SECRET_KEY") SECRET_KEY = "django-insecure-z@+iegqn)^k4^=%rnr-$3*2+gy&7s1=%m$&%h!w_k%m*h+nl=&"
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
...@@ -62,17 +63,6 @@ STATIC_URL = '/static/' ...@@ -62,17 +63,6 @@ STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"] STATICFILES_DIRS = [BASE_DIR / "static"]
TEMPLATES = [ TEMPLATES = [
{ {
<<<<<<< HEAD
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
=======
"BACKEND": "django.template.backends.django.DjangoTemplates", "BACKEND": "django.template.backends.django.DjangoTemplates",
'DIRS': [os.path.join(BASE_DIR, 'templates')], 'DIRS': [os.path.join(BASE_DIR, 'templates')],
"APP_DIRS": True, "APP_DIRS": True,
...@@ -82,7 +72,6 @@ TEMPLATES = [ ...@@ -82,7 +72,6 @@ TEMPLATES = [
"django.template.context_processors.request", "django.template.context_processors.request",
"django.contrib.auth.context_processors.auth", "django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages", "django.contrib.messages.context_processors.messages",
>>>>>>> origin/calendarv2
], ],
}, },
}, },
......
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