Commit 412904bb authored by Brescia Amandy's avatar Brescia Amandy

Merge branch 'forumv2' to 'master'

parents 48d84356 d96ca5b4
from django import forms
from .models import ForumPost
class ForumPostForm(forms.ModelForm):
pub_datetime = forms.DateTimeField(input_formats=['%d/%m/%Y %H:%M'],
widget = forms.DateTimeInput(format = '%d/%m/%Y %H:%M',
attrs = {'class': 'form-control'}),
label = 'Date and Time')
class Meta:
model = ForumPost
fields = '__all__'
\ No newline at end of file
# Generated by Django 3.2 on 2023-05-12 08:38
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_alter_forumpost_author'),
]
operations = [
migrations.AlterModelOptions(
name='forumpost',
options={'ordering': ['pub_datetime']},
),
]
......@@ -12,8 +12,14 @@ class ForumPost(models.Model):
)
pub_datetime = models.DateTimeField()
class Meta:
ordering = ['-pub_datetime']
def __str__(self):
return '{}'.format(self.title)
def get_absolute_url(self):
return reverse("forum:forumpostdetails", kwargs={"pk": self.pk})
class Reply(models.Model):
body = models.TextField()
......@@ -23,4 +29,5 @@ class Reply(models.Model):
ForumPost,
on_delete=models.CASCADE
)
\ No newline at end of file
def get_absolute_url(self):
return reverse("forum:replydetails", kwargs={'pk': self.pk})
\ No newline at end of file
{%extends "base.html"%}
{%block title %}Widget's Forum{%endblock%}
{%block header %}Welcome to Widget's Forum!{%endblock %}
{%block body %}
<h2>Forum Posts:</h2>
{% for post in forum%}
<a href="{% url 'forum:forumpostdetails' post.pk%}">
{{post.title}} by {{post.author}}<br>
</a>
{%endfor%}
<br>
<a href="{%url 'forum:forumpostnew' %}">
<input type="button" value= "New Post"></a>
<br>
<br>
<a href= "{%url 'Dashboard:dashboard_view' %}">Dashboard</a><br>
<a href= "{%url 'assignments:assignments' %}">Assignments</a>
<br>
{% endblock%}
\ No newline at end of file
{%extends "base.html"%}
{%block title %}Add Post{%endblock%}
{%block header %}Add a new post:{%endblock %}
{%block body %}
<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"%}
{%block title %}{{object.title}}{%endblock%}
{%block header %}{{object.title}}{%endblock %}
{%block body %}
by {{object.author.first_name}} {{object.author.last_name}}<br>
{{object.pub_datetime}}<br>
{{object.body}}<br>
<br>
<br>
POST REPLIES:
<br>
{% for reply in object.reply_set.all%}
{{reply.author}}<br>
{{reply.pub_datetime}}<br>
{{reply.body}}<br>
<br>
{%endfor%}
<a href="{%url 'forum:forumpostedit' object.pk %}">
<input type="button" value= "Edit Post"></a>
<br>
{% endblock%}
\ No newline at end of file
{%extends "base.html"%}
{%block title %}Edit Post{%endblock%}
{%block header %}Edit Post:{%endblock %}
{%block body %}
<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 .views import index
from . import views
from .views import (ForumViews, ForumPostDetails,
ForumPostNew, ForumPostEdit)
urlpatterns = [
path('', index, name='index'),
path('forum/', views.ForumViews, name='forum'),
path('forum/forumposts/<int:pk>/details/', ForumPostDetails.as_view(), name="forumpostdetails"),
path('forum/forumposts/add/', ForumPostNew.as_view(), name='forumpostnew'),
path('forum/forumposts/<int:pk>/edit/', ForumPostEdit.as_view(), name = 'forumpostedit'),
]
app_name = "forum"
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from django.views.generic import DetailView, CreateView, UpdateView
from .models import ForumPost, Reply
from .forms import ForumPostForm
# Create your views here.
def index(request):
return_string = '<body> <ul>'
def ForumViews(request):
forum = ForumPost.objects.all()
context = {'forum':forum}
return render(request, 'forum.html', context)
class ForumPostDetails(DetailView):
template_name= "forumpost-details.html"
model = ForumPost
class ForumPostNew(CreateView):
form_class = ForumPostForm
template_name = "forumpost-add.html"
for post in ForumPost.objects.all():
return_string += '<li>{} by {} posted {}:<br>{}</li>'.format(
post.title, post.author, post.pub_datetime.strftime('%m/%d/%Y %H:%M %p'), post.body
)
for replypost in Reply.objects.all():
if replypost.forum_post == post:
return_string += '<li>Reply by {} posted {}:<br>{}</li>'.format(
replypost.author, replypost.pub_datetime.strftime('%m/%d/%Y %I:%M %p'), replypost.body
)
return_string += '<br>'
return_string += '</ul></body>'
def get_success(self):
return reverse('forum:forumpostnew', kwargs = {'pk': self.object.id},
current_app=self.request.resolver_match.namespace)
html_string = '<html>{}</html>'.format(return_string)
class ForumPostEdit(UpdateView):
form_class = ForumPostForm
template_name = "forumpost-edit.html"
queryset = ForumPost.objects.all()
return HttpResponse(html_string)
\ No newline at end of file
def get_success_url(self):
return reverse('forum:forumpostdetails', kwargs = {'pk': self.object.pk},
current_app=self.request.resolver_match.namespace)
\ 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