Commit 0d3f27d7 authored by RJC's avatar RJC

switched to function based view for HttpResponse requirement and added helper...

switched to function based view for HttpResponse requirement and added helper function to convert utc datetime object to local time
parent 39adbf29
from django.shortcuts import render
from django.views import generic
from .models import ForumPost, Reply
# Create your views here.
class ForumPostListView(generic.ListView):
model = ForumPost
template_name = 'forum/forumpost_list.html'
queryset = ForumPost.objects.all()
context_object_name = 'forumposts'
from django.http import HttpResponse
from .models import ForumPost
import pytz
from django.utils import timezone
# helper function to convert utc datetime object to local time
def convert_utc_to_local(utctime):
datetime_format = '%d/%m/%Y %H:%M'
utc = utctime.replace(tzinfo=pytz.UTC)
localtz = utc.astimezone(timezone.get_current_timezone())
return localtz.strftime(datetime_format)
def forum_post_list_view(request):
html_string_1 = '<html lang="en"><head><meta charset="UTF-8"><title>Forum Post List</title>' \
'<h1>Forum Post List</h1></head><ul>'
html_string_2 = ''
for fp in ForumPost.objects.all():
html_string_2 += '<li><b style="font-size: 30px">{}</b>' \
' by <b style="font-size: large">{} {}</b>' \
' posted <b>{}</b><p>{}</p><ul>'.format(fp.title,
fp.author.first_name,
fp.author.last_name,
convert_utc_to_local(fp.pub_datetime), fp.body)
for replies in fp.reply.all():
html_string_2 += '<li> Reply by <b>{} {}</b> ' \
'posted <b>{}</b><p>{}</p></li>'.format(replies.author.first_name,
replies.author.last_name,
convert_utc_to_local(replies.pub_datetime),
replies.body)
html_string_2 += '</ul></li>'
html_string_final = html_string_1 + html_string_2 + '</ul></html>'
return HttpResponse(html_string_final)
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