Commit 40f4dcff authored by Jersey Dayao's avatar Jersey Dayao 🏀

modified views.py and urls.py to map posts/<>/details to corresponding post...

modified views.py and urls.py to map posts/<>/details to corresponding post and added template and static files for web display
parent 2a6d469f
# Generated by Django 4.0.3 on 2022-05-25 20:23
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_post_author_reply'),
]
operations = [
migrations.AlterField(
model_name='reply',
name='reply_post',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='replies', to='forum.post'),
),
]
......@@ -15,7 +15,7 @@ class Post(models.Model):
class Reply(models.Model):
reply_body = models.CharField(max_length=10000)
pub_date = models.DateTimeField('date published')
reply_post = models.ForeignKey(Post, on_delete=models.CASCADE, default=None, null=True)
reply_post = models.ForeignKey(Post, related_name="replies", on_delete=models.CASCADE, default=None, null=True)
author = models.ForeignKey('homepage.WidgetUser', on_delete=models.CASCADE, default=None, null=True)
def __str__(self):
......
h1 {
color: #3E2723;
font-weight: bold;
font-size: 70px;
font-family: Garamond, serif;
margin-bottom: 0px;
}
h2, li {
font-family: Garamond, serif;
color: #3E2723;
}
h3 {
color: #4E342E;
font-family: 'Times New Roman', serif;
}
p {
color:#A1887F;
font-weight: bold;
}
body {
background-color: #BCAAA4;
padding-left: 60px;
padding-right: 60px;
}
a {
color: #212121;
font-size: 20px;
list-style: none;
}
img {
border-radius: 50px;
max-width: 200px;
}
\ No newline at end of file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static "forum/style.css" %}">
<title>Widget's Forum</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
\ No newline at end of file
{% extends "forum/base.html"%}
{% load static %} <!-- need to load pictures ig -->
{% block content %}
<h1>{{ post.post_title }}</h1>
<p>by {{ post.author.first_name }} {{ post.author.last_name }}, {{ post.pub_date }}</p>
<br/>
<h3>{{ post.post_body }}</h3>
<div>
{% if post.id == 1 %}
<img src = "{% static 'forum/images/pepeWOW.jpeg' %}" alt = ":o">
{% elif post.id == 2 %}
<img src = "{% static 'forum/images/pepeBLUSH.jpeg' %}" alt = "hehe>">
{% else %}
<img src = "{% static 'forum/images/pepeTIRED.jpeg' %}" alt = "profile image">
{% endif %}
</div>
<br/>
<br/>
<h2>Comments</h2>
{% if not post.replies.all %}
No Comments Yet. :|
{% else %}
{% for reply in post.replies.all reversed %}
<li>
{{ reply.author.first_name }} {{ reply.author.last_name }},
dated {{ reply.pub_date }} -
{{ reply.reply_body }}
</li>
{% endfor %}
{% endif %}
{% endblock %}
\ No newline at end of file
{% extends "forum/base.html" %}
{% block content %}
<h1>Welcome to Widget's Forum! :)</h1>
<br/>
<h2>Forum Posts:</h2>
{% if post_list %}
<u1>
{% for post in post_list %}
<ol><a href="/forum/{{ post.id }}/details/">{{ post.post_title }} by {{ post.author.first_name }} {{ post.author.last_name }} dated {{ post.pub_date }}</a></ol>
{% endfor %}
</u1>
{% else %}
<p>No posts are available.</p>
{% endif %}
{% endblock %}
\ No newline at end of file
......@@ -2,7 +2,8 @@ from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="Forum")
path("", views.index, name="Forum"),
path("<int:post_id>/details/", views.details, name="details")
]
app_name = "forum"
\ No newline at end of file
from django.http import HttpResponse
from .models import Post, Reply
from django.http import Http404, HttpResponse
from django.shortcuts import render
from .models import Post
# Create your views here.
# forum/
def index(request):
post_data = Post.objects.all()
html = '<html><body><h3>FORUM POSTS:</h3>'
for a in post_data:
html += "{} by {} {} dated {}:<br>{}<br>".format(
a.post_title,
a.author.first_name,
a.author.last_name,
a.pub_date,
a.post_body
)
reply_data = Reply.objects.filter(reply_post=a.id)
for b in reply_data:
html += "Reply by {} {} dated {}:<br>{}<br>".format(
b.author.first_name,
b.author.last_name,
b.pub_date,
b.reply_body
)
html += '<br>'
html += '</body></html>'
post_list = Post.objects.order_by("pub_date")
context = {
"post_list": post_list,
}
return render(request, "forum/index.html", context)
return HttpResponse(html)
\ No newline at end of file
# posts/<post_id>/details
def details(request, post_id):
try:
post = Post.objects.get(pk=post_id)
except Post.DoesNotExist:
raise Http404("Post does not exist.")
return render(request, "forum/detail.html", {"post": post})
\ 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