Commit cc5eb49e authored by Mario Franco C. Deuna's avatar Mario Franco C. Deuna

Updated views.py and urls.py to CBV and created templates.

parent 6e684a91
# Generated by Django 4.0.3 on 2022-05-22 14:39
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0002_rename_announcement_name_reaction_announcement'),
]
operations = [
migrations.AlterField(
model_name='reaction',
name='announcement',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reaction', to='announcements.announcement'),
),
migrations.AlterField(
model_name='reaction',
name='reaction_name',
field=models.CharField(choices=[('Like', 'Like'), ('Love', 'Love'), ('Angry', 'Angry')], default='Like', max_length=10),
),
]
......@@ -4,4 +4,4 @@ from .views import Assignment
class AssignmentForm(ModelForm):
class Meta:
model = Assignment
fields = '__all__'
\ No newline at end of file
fields = '__all__'
<h1>
{{post.post_title}}
</h1>
<h4>
{{post.post_author.first_name}} {{post.post_author.last_name}}, {{post.pub_date.day}}/{{post.pub_date.month}}/{{post.pub_date.year}}
</h4>
<h3>
{{post.post_body}}
</h3>
<img src= {{post.post_imageUrl}}>
<ul>
{% for reply in reply_list %}
{% if reply.reply_post.post_title == post.post_title %}
<li>
<u>
Reply by {{reply.reply_author.first_name}} {{reply.reply_author.last_name}} dated {{reply.pub_date.day}}/{{reply.pub_date.month}}/{{reply.pub_date.year}}:
</u>
<br>
{{reply.reply_body}}
</li>
<br>
{% endif %}
{% endfor %}
</ul>
{% extends 'base.html' %}
{% block content %}
<h1>Welcome to the Widget's Forum!</h1>
{% if object_list %}
<ul>
{% for post in object_list %}
<h3>
<li>
<a href ="{% url 'forum:forum-detail' post.id %}">{{post.post_title}}</a>
<br> by {{post.post_author.first_name}} {{post.post_author.last_name}} dated {{post.pub_date.day}}/{{post.pub_date.month}}/{{post.pub_date.year}}
</li>
</h3>
{% endfor %}
</ul>
{% else %}
<p>
No posts available.
</p>
{% endif %}
{% endblock content %}
from django.urls import path
from .views import forum
from .views import ForumListView, ForumDetailView
urlpatterns = [
path('', forum, name='forum'),
path('', ForumListView.as_view(), name='post-list'),
path('<int:pk>/details/', ForumDetailView.as_view(), name='forum-detail'),
]
app_name = 'forum'
from django.shortcuts import render
from django.http import HttpResponse
from django.http import Http404, HttpRequest, HttpResponse
from .models import Post, Reply
from homepage.models import WidgetUser
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView
from .forms import PostForm
def get_reply():
text=''
for reply in Reply.objects.all():
text += (
'Reply by {} {} '.format(reply.author.first_name,
reply.author.last_name)
+ 'dated {}').format(reply.pub_date.strftime('%x')
+ ':<br>'
+ '{}<br>'.format(reply.reply_body)
)
return text
def forum(request):
text = '<h1>FORUM POSTS:</h1>'
for post in Post.objects.all():
text += (
'{} '.format(post.post_title)
+ 'by {} {} '.format(post.author.first_name,
post.author.last_name)
+ 'dated {}').format(post.pub_date.strftime('%x')
+ ':<br>'
+ '{}<br>'.format(post.post_body)
+ get_reply()
+ '<br>'
)
return HttpResponse(text)
#def get_reply():
#
# text=''
# for reply in Reply.objects.all():
# text += (
# 'Reply by {} {} '.format(reply.author.first_name,
# reply.author.last_name)
# + 'dated {}').format(reply.pub_date.strftime('%x')
# + ':<br>'
# + '{}<br>'.format(reply.reply_body)
# )
# return text
#
#def forum(request):
#
# text = '<h1>FORUM POSTS:</h1>'
#
# for post in Post.objects.all():
# text += (
# '{} '.format(post.post_title)
# + 'by {} {} '.format(post.author.first_name,
# post.author.last_name)
# + 'dated {}').format(post.pub_date.strftime('%x')
# + ':<br>'
# + '{}<br>'.format(post.post_body)
# + get_reply()
# + '<br>'
# )
#
# return HttpResponse(text)
class ForumListView(ListView):
model = Post
ordering = ["-pub_date"]
def index(request):
Replies = Reply.objects.all()
post_list = Post.objects.order_by("-pub_date")
context ={
'Post':Posts,
'Reply':Replies,
'post_list':post_list
}
return render(request, "Forum/forum_detail.html", context)
class ForumDetailView(DetailView):
model = Post
def posts(request, post_id):
reply_list = Reply.objects.order_by("-pub_date")
try:
post = Post.objects.get(pk=post_id)
except Post.DoesNotExist:
raise Http404("post doesnt exist")
return render(request,"Forum/forum_list.html",{"post": post, 'reply_list':reply_list})
def PostCreate(request):
form = PostForm()
context = {
'form':form,
}
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
form.save()
return redirect('/posts')
return render(request, 'Forum/post_add.html', context)
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