Commit d514fa40 authored by Isaiah Flores's avatar Isaiah Flores

Changes for Lab 3 and Final Project

Lab 3
- changed views to class based views
- added get_absolute_url() and get_author_name() functions to Post in models.py
- added classes for ListView and DetailView for Post
- adjusted URLs for the pages
- added HTML templates in templates/forum/

Final Project
- added addPost() function in views.py
- added new forms.py file
- added new URL for addPost() page
- added post_form.html to templates
parent abed8ca5
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = ['post_title', 'post_body', 'author'] # Selected fields for the form
\ No newline at end of file
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.core.validators import RegexValidator
# Using WidgetUser from homepage # Using WidgetUser from homepage
from homepage.models import WidgetUser from homepage.models import WidgetUser
class Post(models.Model): class Post(models.Model):
post_title = models.CharField(max_length=50) post_title = models.CharField('Title', max_length=100)
post_body = models.CharField(max_length=100) post_body = models.CharField('Body', max_length=1000)
pub_date = models.DateTimeField(auto_now_add=True, editable=False) pub_date = models.DateTimeField(auto_now_add=True, editable=False)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
# author_name = WidgetUser.objects.get(fk=author).full_name()
# Takes URL to ForumDetailView
def get_absolute_url(self):
return reverse('Details', args=[(self.pk)])
def __str__(self): def get_author_name(self):
return '{} by {} dated {}:\n {}'.format(self.post_title, self.author.full_name(), self.pub_date, self.post_body) return str(self.author.first_name + " " + self.author.last_name)
def get_absolute_url(self): author_name = get_author_name
return reverse('forum', args=[(self.post_title)])
class Reply(models.Model): class Reply(models.Model):
reply_body = models.CharField(max_length=100) reply_body = models.CharField(max_length=500)
pub_date = models.DateTimeField(auto_now_add=True, editable=False) pub_date = models.DateTimeField(auto_now_add=True, editable=False)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
replied_post = models.ForeignKey(Post, on_delete=models.CASCADE) replied_post = models.ForeignKey(Post, related_name = 'replies', on_delete=models.CASCADE)
def get_author_name(self):
return str(self.author.first_name + " " + self.author.last_name)
def __str__(self): author_name = get_author_name
return '\nReply by {} dated {}:\n {}'.format(self.author.full_name(), self.pub_date, self.reply_body) \ No newline at end of file
\ No newline at end of file
{% block content %}
<h3>{{post.post_title}}</h3>
<p>by {{post.author_name}}, {{post.pub_date|date:"d/m/Y"}}<p>
{{post.post_body}}
<ul>
{% for reply in post.replies.all %}
<li>
{{reply.author_name}}, {{reply.pub_date|date:"d/m/Y"}}:<br>
{{reply.reply_body}}
</li>
{% endfor %}
</ul>
<a href="{% url 'Forum' %}">Back to Forum</a>
{% endblock %}
\ No newline at end of file
{% block content %}
<h3>New Forum Post</h3>
<form action="{% url 'Add' %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Post">
</form>
<a href="{% url 'Forum' %}">Back to Forum</a>
{% endblock %}
\ No newline at end of file
{% block content %}
{% block title %}Welcome to Widget’s Forum!{% endblock %}
<p>Forum posts:</p>
{% if post_list %}
<ul>
{% for post in post_list %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.post_title }}</a> by {{post.author_name}} dated {{post.pub_date|date:"d/m/Y"}}<br>
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no posts.</p>
{% endif %}
<p><a href="{% url 'Add' %}">New Forum Post</a></p>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import forum from . import views
from .views import ForumListView, ForumDetailView
urlpatterns = [ urlpatterns = [
path('', forum, name='forum') path('', ForumListView.as_view(), name='Forum'),
path('<int:pk>/details/', ForumDetailView.as_view(), name='Details'),
path('add/', views.addPost, name='Add'),
] ]
\ No newline at end of file
from django.http import HttpResponse from django.views.generic import ListView, DetailView
from .models import Post, Reply from django.shortcuts import render, redirect
from django.db import models
from .models import Post
from .forms import PostForm
def forum(request): class ForumListView(ListView):
post = Post.objects.all() queryset = Post.objects.order_by('-pub_date') # Orders publication by most recent
reply = Reply.objects.all() context_object_name = "post_list"
output = "FORUM POSTS: \n" + "\n".join([str(x) for x in post]) + "\n".join([str(z) for z in reply]) template_name = "post_list.html"
return HttpResponse(output, content_type="text/plain")
class ForumDetailView(DetailView):
model = Post
template_name = "forum/post_detail.html"
def addPost(request):
form = PostForm()
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
form.pub_date = models.DateTimeField(auto_now_add=True, editable=False) # Sets current time as pub_date
new_post = form.save() # Creates new post
return redirect('Details', pk=new_post.pk) # Redirects to detailed view of new post
else:
form = PostForm()
context = {'form':form}
return render(request,'forum/post_form.html', context) # Takes post_form template and displays form and post button
\ 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