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):
post_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published', auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True)
image = models.ImageField(upload_to='images/', null=True, blank=True)
def __str__(self):
return self.post_title
......@@ -15,5 +16,3 @@ class Reply(models.Model):
reply_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published', auto_now_add=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;
font-weight: bold;
}
h4 {
h3 {
color: blue;
font-weight: lighter;
}
p {
h4 {
color:green;
font-weight: bold;
}
p {
color: brown;
}
body {
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 @@
{% block content %}
<h3>Welcome to Widget's Forum!</h3>
<h4>Forum Posts:</h4>
{% if posts_list %}
{% if object_list %}
<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>
{% endfor %}
</ul>
{% else %}
<p>No posts are available.</p>
{% endif %}
<a href="posts/add/">
<input type="button" value="New Forum Post">
</a>
{% endblock %}
\ No newline at end of file
......@@ -3,9 +3,9 @@
{% block title %}Widget's Forum{% endblock %}
{% block content %}
<h3>{{ post.post_title }}</h3>
<h4>by {{ post.author.first_name }} {{ post.author.last_name }}, {{ post.pub_date|date:"d/m/Y" }}</h4>
<p>{{ post.post_body }}</p>
<h2>{{ post.post_title }}</h2>
<h3>by {{ post.author.first_name }} {{ post.author.last_name }}, {{ post.pub_date|date:"d/m/Y" }}</h3>
<h4>{{ post.post_body }}</h4>
{% if replies_list %}
<ul>
{% for i in replies_list %}
......@@ -13,8 +13,9 @@
{% endfor %}
</ul>
{% 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 %}
{% load static %}
<img src="{% static image_url %}" alt="Photo of Post"/>
{% endblock %}
from django.urls import path
from . import views
from .views import ForumView, PostDetailsView, FormView
urlpatterns = [
#forum
path('', views.forumView, name='forum'),
#post/<post_id>/details/
path('posts/<int:post_id>/details/', views.postDetailsView, name='post details')
#note to self: id of posts are 3,4,5,6 according to shell query
path('', ForumView.as_view(), name='forum'),
#posts/<post_id>/details/
path('posts/<int:post_id>/details/', PostDetailsView.as_view(), name='post details'),
#posts/add/
path('posts/add/', views.FormView, name='form')
]
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views import View
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):
posts_list = Post.objects.order_by("-pub_date")
context = {
"posts_list": posts_list
}
class ForumView(ListView):
model = Post
ordering = ['-pub_date']
template_name='forum.html'
return render(request, "forum.html", context)
def postDetailsView(request, post_id):
class PostDetailsView(View):
def get(self, request, post_id):
displayed_post = Post.objects.get(pk=post_id)
replies_list = Reply.objects.filter(post=displayed_post).order_by("-pub_date")
image_url = f'forum/{displayed_post.id}.png'
context = {
"post": displayed_post,
"replies_list": replies_list,
"image_url": image_url
}
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
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# 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.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('homepage/', include('homepage.urls')),
......@@ -22,4 +9,4 @@ urlpatterns = [
path('forum/', include('forum.urls')),
path('assignments/', include('assignments.urls')),
path('admin/', admin.site.urls),
]
\ No newline at end of file
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
\ 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