Commit f9e6a556 authored by enricosuplico's avatar enricosuplico

updated forumV2

parent f1e19060
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
from django.utils import timezone
# Create your models here. # Create your models here.
...@@ -12,6 +14,16 @@ class ForumPost(models.Model): ...@@ -12,6 +14,16 @@ class ForumPost(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def save(self, *args, **kwargs):
if not self.pk:
self.pub_datetime = timezone.now()
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('forum:forumpost-details', kwargs={'pk': self.pk})
class Reply(models.Model): class Reply(models.Model):
body = models.TextField(default = "", null = True,) body = models.TextField(default = "", null = True,)
author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE, default = "", null = True,) author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE, default = "", null = True,)
......
{% extends 'base.html' %}
{% block title %} Widget's Forum {% endblock %}
{% block content %}
<h1>Welcome to Widget's Forum!</h1>
<h3>Forum Posts:</h3>
{% for forum in forums %}
<a href="{% url 'forum:forumpost-details' forum.id %}"> {{ forum.title }}
by {{ forum.author.first_name }} {{ forum.author.last_name }}
</a> <br>
{% endfor %} <br>
<a href="{% url 'forum:forumpost-add' %}">
<button>New Post</button>
</a> <br><br>
<a href="">Dashboard</a> <br>
<a href="">Announcements</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Add Post {% endblock %}
{% block content %}
<h1> Add a new post</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Add Post">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{ forumpost.title }}{% endblock %}
{% block content %}
<h1>{{ forumpost.title }} <br>
by {{ forumpost.author.first_name }}
{{ forumpost.author.last_name }}
</h1>
{{ forumpost.pub_datetime }} <br>
{{ forumpost.body }} <br> <br>
Post Replies: <br>
{% for reply in replies %}
<br> by {{ reply.author.first_name }}
{{ reply.author.last_name }}
{{ reply.pub_datetime }} <br>
{{ reply.body }} <br>
{% endfor %} <br>
<a href="{% url 'forum:forumpost-edit' object.id %}">
<button>Edit Post</button>
</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Edit Post {% endblock %}
{% block content %}
<h1> Edit post:</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from.import views from.views import Forum, ForumDetailViews, ForumCreateView, ForumEditView
urlpatterns = [ urlpatterns = [
path('', views.forum, name = "forum"), path('', Forum, name="forum"),
] path('<int:pk>/details/', ForumDetailViews.as_view(), name="forumpost-details"),
\ No newline at end of file path('add/', ForumCreateView.as_view(), name="forumpost-add"),
path('<int:pk>/edit/', ForumEditView.as_view(), name="forumpost-edit"),
]
app_name = 'forum'
\ No newline at end of file
from .models import ForumPost, Reply from .models import ForumPost, Reply
from django.http import HttpResponse from django.http import HttpResponse
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.shortcuts import render
# Create your views here. # Create your views here.
def forum(request): def Forum(request):
posts = ForumPost.objects.all() forums = ForumPost.objects.order_by("-pub_datetime")
replies = Reply.objects.all() context = {
response = "Widget's Forums <br> <br>" + "Forum Posts: <br>" "forums": forums,
for post in posts: }
post_name = post.author.first_name + " " + post.author.last_name
response += "<br>" + post.title + " by " + post_name + " posted " + post.pub_datetime.strftime("%a, %b %d, %Y %I:%M %p") + ": <br>" + post.body + "<br>" return render(request, 'forum/forum.html', context)
for reply in replies:
if(post.title == reply.forum_post.title):
reply_name = reply.author.first_name + " " + reply.author.last_name class ForumDetailViews(DetailView):
response += "Reply by " + reply_name + " posted " + reply.pub_datetime.strftime("%a, %b %d, %Y %I:%M %p") + ": <br>" + reply.body + "<br>" model = ForumPost
template_name = 'forum/forumpost-details.html'
return HttpResponse(response)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['replies'] = Reply.objects.filter(forum_post=self.object)
return context
class ForumCreateView(CreateView):
model = ForumPost
fields = 'title', 'body', 'author'
template_name = 'forum/forumpost-add.html'
class ForumEditView(UpdateView):
model = ForumPost
fields = 'title', 'body', 'author'
template_name = 'forum/forumpost-edit.html'
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>{% block title %}{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock%}
</div>
{% block scripts%}{% endblock%}
</body>
</html>
\ No newline at end of file
...@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ ...@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
""" """
from pathlib import Path from pathlib import Path
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
...@@ -57,7 +58,7 @@ ROOT_URLCONF = 'widget_mgabolanimav.urls' ...@@ -57,7 +58,7 @@ ROOT_URLCONF = 'widget_mgabolanimav.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ '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