Commit 7ee9735c authored by Tanya Yotoko's avatar Tanya Yotoko

created branch forumv2, initialized templates, edited views.py, urls.py and models.py

parent cd772cb6
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
class ForumPost(models.Model): class ForumPost(models.Model):
title = models.CharField(max_length=255,default="") title = models.CharField(max_length=255,default="")
body = models.TextField(default="") body = models.TextField(default="")
author = models.ForeignKey(WidgetUser,on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser,on_delete=models.CASCADE)
pub_datetime = models.DateTimeField() pub_datetime = models.DateTimeField(default=timezone.now)
def __str__(self): def __str__(self):
return '{}'.format(self.title) return '{}'.format(self.title)
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="") body = models.TextField(default="")
author = models.ForeignKey(WidgetUser,on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser,on_delete=models.CASCADE)
......
{% extends 'base.html' %}
{% block title %}Widget's Forum{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}Add Post{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{object.title}}{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}Edit Post{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import (forum, ForumPostDetailView, ForumPostCreateView, ForumPostUpdateView)
from .views import index
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('',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'),
] ]
app_name = "forum" app_name = "forum"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import ForumPost, Reply from .models import ForumPost, Reply
from dashboard.models import WidgetUser from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
def forum(request):
return render(request, 'forum/forum.html', {'forum':ForumPost.objects.order_by('-pub_datetime')
})
def index(request): class ForumPostDetailView(DetailView):
model = ForumPost
template_name = "forum/forumpost-details.html"
return_string = '<body>' class ForumPostCreateView(CreateView):
for post in ForumPost.objects.all(): model = ForumPost
fields = ['title','body','author']
template_name = 'forum/forumpost-add.html'
post_heading = '{} by {} {} posted {}:<br>'.format( class ForumPostUpdateView(UpdateView):
post.title, model = ForumPost
post.author.first_name, fields = ['title','body','author']
post.author.last_name, template_name = 'forum/forumpost-edit.html'
post.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
)
post_body = '{}<br>'.format(post.body)
post_replies = ''
for reply in Reply.objects.filter(forum_post=post):
post_replies+= 'Reply by {} {} posted {}:<br>'.format(
reply.author.first_name,
reply.author.last_name,
reply.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
)
post_replies += '{}<br>'.format(reply.body)
return_string += post_heading + post_body + post_replies + '<br>'
html_string ='<html>{}</html>'.format(return_string)
return HttpResponse('Widget’s Forum<br><br>Forum Posts:<br>' + html_string)
\ 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