Commit 3a79eb7e authored by Ron Rodillas's avatar Ron Rodillas

added datetime formatting function for the models and created the view function

parent 76fb520d
No preview for this file type
...@@ -15,4 +15,4 @@ class ReplyAdmin(admin.ModelAdmin): ...@@ -15,4 +15,4 @@ class ReplyAdmin(admin.ModelAdmin):
list_filter = ('pub_datetime',) list_filter = ('pub_datetime',)
admin.site.register(ForumPost, ForumPostAdmin) admin.site.register(ForumPost, ForumPostAdmin)
admin.site.register(Reply,ReplyAdmin) admin.site.register(Reply, ReplyAdmin)
\ No newline at end of file \ No newline at end of file
# Generated by Django 4.1.7 on 2023-03-05 11:48
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='reply',
name='reply_to',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='forum.forumpost'),
preserve_default=False,
),
]
...@@ -7,7 +7,17 @@ class ForumPost (models.Model): ...@@ -7,7 +7,17 @@ class ForumPost (models.Model):
author = models.CharField(max_length=100) author = models.CharField(max_length=100)
pub_datetime = models.DateTimeField(auto_now_add=True) pub_datetime = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
def datetime_posted(self):
return self.pub_datetime.strftime('%m/%d/%Y, %I:%M %p')
class Reply (models.Model): class Reply (models.Model):
body = models.TextField() body = models.TextField()
author = models.CharField(max_length=100) author = models.CharField(max_length=100)
pub_datetime = models.DateTimeField(auto_now_add=True) pub_datetime = models.DateTimeField(auto_now_add=True)
reply_to = models.ForeignKey(ForumPost, on_delete=models.CASCADE)
def datetime_posted(self):
return self.pub_datetime.strftime('%m/%d/%Y, %I:%M %p')
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse
from .models import ForumPost, Reply
# Create your views here. # Create your views here.
def index(request):
return_string = "Widget's Forums <br> <br> Forum Posts:<br>"
for i in ForumPost.objects.all():
return_string+=(
i.title + " by " + i.author + " posted " + i.datetime_posted() + ":<br>"
+ i.body + "<br>"
)
for n in Reply.objects.all():
if n.reply_to == i:
return_string+=(
"Reply by " + n.author + " posted " + n.datetime_posted() + ":<br>"
+ n.body + "<br>"
)
return_string+="<br>"
return HttpResponse(return_string)
\ No newline at end of file
...@@ -11,9 +11,6 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ ...@@ -11,9 +11,6 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
""" """
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv
import os
load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
......
...@@ -17,6 +17,6 @@ from django.contrib import admin ...@@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import include, path from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('forum/', include('forum.urls', namespace="forum")),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
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