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 dashboard.models import WidgetUser
from django.urls import reverse
# Forum Post
# title; body; author; pub_datetime;
......@@ -11,6 +12,9 @@ class ForumPost(models.Model):
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('forum:forumpost_details', kwargs={'pk':self.pk})
# Reply
# body; author; pub_datetime; forum_post;
......
{% 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
# url for forum
urlpatterns = [
path("", views.forumIndex, name="forumIndex"),
]
\ No newline at end of file
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
# forum view from .models
def forumIndex(request):
# fetch forum posts and replies
forum = ForumPost.objects.all()
# dashboard view from .models
def forum_view(request):
forumposts = ForumPost.objects.all()
replies = Reply.objects.all()
forum_output = "Widget's Forum <br><br> Forum Posts:<br>"
for post in forum:
forum_output = (forum_output + post.title + " by " + post.author.first_name + " " + post.author.last_name +
" posted " + post.pub_datetime.strftime('%m/%d/%Y, %I:%M %p') + ":<br>" +
post.body + "<br>"
)
for reply in replies:
if reply.forum_post.title == post.title:
forum_output = (forum_output + "Reply by " + reply.author.first_name + " " + reply.author.last_name
+ " posted " + reply.pub_datetime.strftime('%m/%d/%Y, %I:%M %p') + ":<br>" +
reply.body + "<br>"
)
forum_output = forum_output + "<br>"
return HttpResponse(forum_output)
return render(request, 'forum.html', {'forumposts': forumposts, 'replies': replies})
class ForumPostDetailView(DetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['replies'] = Reply.objects.all()
return context
model = ForumPost
template_name = "forumpost-details.html"
class ForumPostCreateView(CreateView):
model = ForumPost
template_name = "forumpost-add.html"
fields = "__all__"
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/
from pathlib import Path
import os
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......@@ -22,7 +23,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
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!
......@@ -62,17 +63,6 @@ STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
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",
'DIRS': [os.path.join(BASE_DIR, 'templates')],
"APP_DIRS": True,
......@@ -82,7 +72,6 @@ TEMPLATES = [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"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