Commit 4ddda822 authored by Rac Gerard Elizaga's avatar Rac Gerard Elizaga

Added form to forums app, Added images to forum post object, Edited settings to enable images

parent df1cefa4
# Generated by Django 4.0.3 on 2022-05-21 06:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assignments', '0006_assignment_passing_score'),
]
operations = [
migrations.AlterField(
model_name='course',
name='course_code',
field=models.CharField(max_length=10, unique=True),
),
migrations.AlterField(
model_name='course',
name='course_title',
field=models.CharField(max_length=500, unique=True),
),
]
# Generated by Django 4.0.3 on 2022-05-21 06:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assignments', '0007_alter_course_course_code_alter_course_course_title'),
]
operations = [
migrations.AlterField(
model_name='course',
name='course_code',
field=models.CharField(max_length=10),
),
migrations.AlterField(
model_name='course',
name='course_title',
field=models.CharField(max_length=500),
),
]
# Generated by Django 4.0.3 on 2022-05-21 06:48
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assignments', '0008_alter_course_course_code_alter_course_course_title'),
]
operations = [
migrations.AlterField(
model_name='course',
name='course_code',
field=models.CharField(max_length=10, unique=True),
),
migrations.AlterField(
model_name='course',
name='course_title',
field=models.CharField(max_length=500, unique=True),
),
]
from django import forms
from homepage.models import WidgetUser
from .models import Post, Reply
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = '__all__'
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-05-21 06:48
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0006_alter_post_pub_date_alter_reply_pub_date'),
]
operations = [
migrations.AddField(
model_name='post',
name='image',
field=models.ImageField(blank=True, null=True, upload_to='images/'),
),
]
...@@ -6,6 +6,7 @@ class Post(models.Model): ...@@ -6,6 +6,7 @@ class Post(models.Model):
post_body = models.CharField(max_length=1000) post_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published', auto_now_add=True) pub_date = models.DateTimeField('date published', auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True)
image = models.ImageField(upload_to='images/', null=True, blank=True)
def __str__(self): def __str__(self):
return self.post_title return self.post_title
...@@ -15,5 +16,3 @@ class Reply(models.Model): ...@@ -15,5 +16,3 @@ class Reply(models.Model):
reply_body = models.CharField(max_length=1000) reply_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published', auto_now_add=True) pub_date = models.DateTimeField('date published', auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True)
\ No newline at end of file
#ADD IMAGE TO POSTDETAILS PAGE
\ No newline at end of file
h3 { h2 {
color: orangered; color: orangered;
font-weight: bold; font-weight: bold;
} }
h4 { h3 {
color: blue; color: blue;
font-weight: lighter; font-weight: lighter;
} }
p { h4 {
color:green; color:green;
font-weight: bold; font-weight: bold;
} }
p {
color: brown;
}
body { body {
background-color: aquamarine; background-color: aquamarine;
} }
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Forum{% endblock %}
{% block content %}
<h3>New Forum Post</h3>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input type='submit' value='Save Post'>
</form>
<a href="../../">
<input type="button" value="Back to Posts List">
</a>
{% endblock %}
\ No newline at end of file
...@@ -5,13 +5,16 @@ ...@@ -5,13 +5,16 @@
{% block content %} {% block content %}
<h3>Welcome to Widget's Forum!</h3> <h3>Welcome to Widget's Forum!</h3>
<h4>Forum Posts:</h4> <h4>Forum Posts:</h4>
{% if posts_list %} {% if object_list %}
<ul> <ul>
{% for i in posts_list %} {% for i in object_list %}
<li><a href="posts/{{ i.id }}/details/">{{ i.post_title}} by {{ i.author.first_name }} {{ i.author.last_name }} dated {{ i.pub_date|date:"d/m/Y" }}</a></li> <li><a href="posts/{{ i.id }}/details/">{{ i.post_title}} by {{ i.author.first_name }} {{ i.author.last_name }} dated {{ i.pub_date|date:"d/m/Y" }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% else %} {% else %}
<p>No posts are available.</p> <p>No posts are available.</p>
{% endif %} {% endif %}
<a href="posts/add/">
<input type="button" value="New Forum Post">
</a>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
{% block title %}Widget's Forum{% endblock %} {% block title %}Widget's Forum{% endblock %}
{% block content %} {% block content %}
<h3>{{ post.post_title }}</h3> <h2>{{ post.post_title }}</h2>
<h4>by {{ post.author.first_name }} {{ post.author.last_name }}, {{ post.pub_date|date:"d/m/Y" }}</h4> <h3>by {{ post.author.first_name }} {{ post.author.last_name }}, {{ post.pub_date|date:"d/m/Y" }}</h3>
<p>{{ post.post_body }}</p> <h4>{{ post.post_body }}</h4>
{% if replies_list %} {% if replies_list %}
<ul> <ul>
{% for i in replies_list %} {% for i in replies_list %}
...@@ -13,8 +13,9 @@ ...@@ -13,8 +13,9 @@
{% endfor %} {% endfor %}
</ul> </ul>
{% else %} {% else %}
<p>No posts are available.</p> <p>No replies are available.</p>
{% endif %}
{% if post.image %}
<img src="{{ post.image.url }}" alt="Photo of Post"/>
{% endif %} {% endif %}
{% load static %}
<img src="{% static image_url %}" alt="Photo of Post"/>
{% endblock %} {% endblock %}
from django.urls import path from django.urls import path
from . import views from . import views
from .views import ForumView, PostDetailsView, FormView
urlpatterns = [ urlpatterns = [
#forum #forum
path('', views.forumView, name='forum'), path('', ForumView.as_view(), name='forum'),
#post/<post_id>/details/ #posts/<post_id>/details/
path('posts/<int:post_id>/details/', views.postDetailsView, name='post details') path('posts/<int:post_id>/details/', PostDetailsView.as_view(), name='post details'),
#posts/add/
#note to self: id of posts are 3,4,5,6 according to shell query path('posts/add/', views.FormView, name='form')
] ]
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.shortcuts import render
from django.views import View from django.views import View
from .models import Post, Reply from .models import Post, Reply
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from.forms import PostForm
def forumView(request): class ForumView(ListView):
posts_list = Post.objects.order_by("-pub_date") model = Post
context = { ordering = ['-pub_date']
"posts_list": posts_list template_name='forum.html'
}
return render(request, "forum.html", context) class PostDetailsView(View):
def get(self, request, post_id):
def postDetailsView(request, post_id):
displayed_post = Post.objects.get(pk=post_id) displayed_post = Post.objects.get(pk=post_id)
replies_list = Reply.objects.filter(post=displayed_post).order_by("-pub_date") replies_list = Reply.objects.filter(post=displayed_post).order_by("-pub_date")
image_url = f'forum/{displayed_post.id}.png'
context = { context = {
"post": displayed_post, "post": displayed_post,
"replies_list": replies_list, "replies_list": replies_list,
"image_url": image_url
} }
return render(request, "post_details.html", context) return render(request, "post_details.html", context)
def FormView(request):
form = PostForm()
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
form.save()
context = {
"form": form
}
return render(request, "form.html", context)
...@@ -124,6 +124,9 @@ USE_TZ = True ...@@ -124,6 +124,9 @@ USE_TZ = True
# https://docs.djangoproject.com/en/4.0/howto/static-files/ # https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# Default primary key field type # Default primary key field type
......
"""widget_group_18 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin from django.contrib import admin
from django.urls import include, path from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('homepage/', include('homepage.urls')), path('homepage/', include('homepage.urls')),
...@@ -22,4 +9,4 @@ urlpatterns = [ ...@@ -22,4 +9,4 @@ urlpatterns = [
path('forum/', include('forum.urls')), path('forum/', include('forum.urls')),
path('assignments/', include('assignments.urls')), path('assignments/', include('assignments.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
\ No newline at end of file \ 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