Commit 3d214b09 authored by Bianca Aguilar's avatar Bianca Aguilar

Finished building form + relevant views for forum app

parent 4d15435a
.env .env
.vscode .vscode'myenv/'
\ No newline at end of file
No preview for this file type
from django import forms
from django.forms import Textarea
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['post_title', 'post_body', 'author', 'image']
widgets = {
'post_body': Textarea(attrs={'placeholder': 'Text goes here', 'cols': 12, 'rows': 12}),
}
labels = {
'post_title': 'Title',
'post_body': 'Body',
}
# Generated by Django 4.0.3 on 2022-05-23 06:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0013_alter_post_image'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(auto_now=True, null=True),
),
]
# Generated by Django 4.0.3 on 2022-05-23 09:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0003_widgetuser_department'),
('forum', '0014_alter_post_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
]
# Generated by Django 4.0.3 on 2022-05-23 09:38
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0015_alter_post_author'),
]
operations = [
migrations.AlterField(
model_name='post',
name='image',
field=models.ImageField(null=True, upload_to='forum/'),
),
]
# Generated by Django 4.0.3 on 2022-05-23 15:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0003_widgetuser_department'),
('forum', '0016_alter_post_image'),
]
operations = [
migrations.AlterField(
model_name='post',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
preserve_default=False,
),
migrations.AlterField(
model_name='post',
name='image',
field=models.ImageField(default=1, upload_to='forum/'),
preserve_default=False,
),
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(auto_now_add=True, null=True),
),
]
# Generated by Django 4.0.3 on 2022-05-23 16:22
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('forum', '0017_alter_post_author_alter_post_image_and_more'),
]
operations = [
migrations.AlterModelOptions(
name='post',
options={'ordering': ['-id']},
),
migrations.AlterModelOptions(
name='reply',
options={'ordering': ['-id']},
),
]
from django.db import models from django.db import models
from django.urls import reverse
# Create your models here. # Create your models here.
class Post(models.Model): class Post(models.Model):
post_title = models.CharField(max_length=50) post_title = models.CharField(max_length=50)
post_body = models.CharField(max_length=500) post_body = models.CharField(max_length=500)
pub_date = models.DateTimeField(auto_now=True) pub_date = models.DateTimeField(auto_now_add=True, null=True)
author = models.ForeignKey( author = models.ForeignKey(
'homepage.WidgetUser', 'homepage.WidgetUser',
on_delete=models.CASCADE on_delete=models.CASCADE
) )
image = models.ImageField(upload_to="forum/") image = models.ImageField(upload_to="forum/")
class Meta:
ordering = ['-id']
def __str__(self): def __str__(self):
return '{}'.format(self.post_title) return '{}'.format(self.post_title)
@property def get_absolute_url(self):
def post_detail(self): return reverse('forum:post-detail', kwargs={'pk': self.pk})
post = '<br>{} by {} dated {}:'.format(self.post_title, self.author.forum_name, self.pub_date)
post += '<br>{}'.format(self.post_body)
return post
class Reply(models.Model): class Reply(models.Model):
...@@ -36,17 +38,8 @@ class Reply(models.Model): ...@@ -36,17 +38,8 @@ class Reply(models.Model):
related_name="comments" related_name="comments"
) )
class Meta:
ordering = ['-id']
def __str__(self): def __str__(self):
return 'Reply dated {}'.format(self.pub_date) return 'Reply dated {}'.format(self.pub_date)
@property
def reply_detail(self):
reply = '<br>Reply by {} {} dated {}:'.format(self.author.first_name, self.author.last_name, self.pub_date)
reply += '<br>{}'.format(self.reply_body)
return reply
@property
def associated_post(self):
return '{}'.format(self.post.__str__)
...@@ -8,13 +8,21 @@ ...@@ -8,13 +8,21 @@
{% block app_header %}Welcome to Widget's Forum!{% endblock %} {% block app_header %}Welcome to Widget's Forum!{% endblock %}
{% block content %} {% block content %}
<ul class="posts"> {% if post_list %}
{% for p in post_list %} <div class="wrapper list">
<li> <ul class="posts">
<a href="{% url 'forum:post-detail' pk=p.pk %}"> {% for p in post_list %}
<p><span class="title">{{ p.post_title }}</span> <br/> by <span class="author">{{ p.author.first_name }} {{ p.author.last_name }}</span> dated {{ p.pub_date|date:"d/m/Y" }}</p> <li>
</a> <a href="{% url 'forum:post-detail' pk=p.pk %}">
</li> <p><span class="title">{{ p.post_title }}</span> <br/> by <span class="author">{{ p.author.first_name }} {{ p.author.last_name }}</span> dated {{ p.pub_date|date:"d/m/Y" }}</p>
{% endfor %} </a>
</ul> </li>
{% endblock %} {% endfor %}
\ No newline at end of file </ul>
<a class="button main" href="{% url 'forum:post-create' %}">New Forum Post</a>
{% else %}
<p>No articles are available.</p>
<a class="button main" href="{% url 'forum:post-create' %}">New Forum Post</a>
</div>
{% endif %}
{% endblock %}
\ No newline at end of file
...@@ -6,20 +6,26 @@ ...@@ -6,20 +6,26 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div class="wrapper"> <div class="wrapper detail">
<div class="post"> <div class="post">
<h1>{{ object.post_title }}</h1> <h1>{{ object.post_title }}</h1>
<h2>by {{ object.author.first_name }} {{ object.author.last_name }}, {{ object.pub_date|date:"d/m/Y" }}</h2> <h2>by {{ object.author.first_name }} {{ object.author.last_name }}, {{ object.pub_date|date:"d/m/Y" }}</h2>
<p>{{ object.post_body }}</p> <p>{{ object.post_body }}</p>
<img src="{{ object.image.url }}"> {% if object.image %}
<img src="{{ object.image.url }}">
{% endif %}
</div> </div>
<ul class="replies"> {% if reply_list %}
{% for r in object.comments.all %} <ul class="replies">
<li> {% for r in object.comments.all %}
<p><span class="author">{{ r.author.first_name }} {{ r.author.last_name }}</span>, {{ r.pub_date|date:"d/m/Y" }}: {{ r.reply_body }}</p> <li>
</li> <p><span class="author">{{ r.author.first_name }} {{ r.author.last_name }}</span>, {{ r.pub_date|date:"d/m/Y" }}: {{ r.reply_body }}</p>
{% endfor %} </li>
</ul> {% endfor %}
{% else %}
<p>No replies have been posted yet.</p>
</ul>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block styles %}
<link rel="stylesheet" href="{% static 'css/forum_stylesheet.css' %}">
{% endblock %}
{% block app_header %}New Forum Post{% endblock %}
{% block content %}
<div class="wrapper">
<form class="post" method="post" enctype="multipart/form-data">
{% csrf_token %}
<ul>
{{ form.as_p }}
</ul>
<input class="button main" type="submit" value="Save Post">
<br/>
<a class="button secondary" href="{% url 'forum:index' %}">Back to Forum Posts</a>
</form>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block styles %}
<link rel="stylesheet" href="{% static 'css/forum_stylesheet.css' %}">
{% endblock %}
{% block app_header %}Welcome to Widget's Forum!{% endblock %}
{% block content %}
{% if post_list %}
<div class="wrapper list">
<ul class="posts">
{% for p in post_list %}
<li>
<a href="{% url 'forum:post-detail' pk=p.pk %}">
<p><span class="title">{{ p.post_title }}</span> <br/> by <span class="author">{{ p.author.first_name }} {{ p.author.last_name }}</span> dated {{ p.pub_date|date:"d/m/Y" }}</p>
</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No articles are available.</p>
</div>
{% endif %}
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import ForumPageView, PostDetailView from forum.models import Post
from .views import ForumPageView, PostListView, PostDetailView, PostCreateView, PostUpdateView
urlpatterns = [ urlpatterns = [
path('', ForumPageView.as_view(), name='index'), path('', ForumPageView.as_view(), name='index'),
# posts/
path('posts/', PostListView.as_view(), name='post-list'),
# post/1/details
path('post/<int:pk>/details', PostDetailView.as_view(), name='post-detail'), path('post/<int:pk>/details', PostDetailView.as_view(), name='post-detail'),
# post/add
path('posts/add', PostCreateView.as_view(), name='post-create'),
# post/update
path('post/<int:pk>/update', PostUpdateView.as_view(), name='post-update')
] ]
app_name = "forum" app_name = "forum"
\ No newline at end of file
from django.shortcuts import render from logging import setLogRecordFactory
from django.shortcuts import render, redirect
from django.views import View from django.views import View
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Post, Reply from .models import Post, Reply
from .forms import PostForm
# Create your views here. # Create your views here.
class ForumPageView(View): class ForumPageView(View):
model = Post
def get(self, request): def get(self, request):
return render(request, 'forum/forum.html', { return render(request, 'forum/forum.html', {
'post_list': Post.objects.all().order_by('-id'), 'post_list': Post.objects.all(),
'reply_list': Reply.objects.all().order_by('-id') 'reply_list': Reply.objects.all(),
'form': PostForm()
}) })
def post(self, request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
new_post = form.save()
return redirect('post_detail', pk=new_post.pk)
else:
form = PostForm()
return render(request, 'forum/post_list.html', {'form': form})
class PostListView(ListView):
model = Post
class PostDetailView(DetailView): class PostDetailView(DetailView):
model = Post model = Post
class PostCreateView(CreateView):
model = Post
fields = ['post_title', 'post_body', 'author', 'image']
form = PostForm
class PostUpdateView(UpdateView):
model = Post
fields = ['post_title', 'post_body', 'author', 'image']
form = PostForm
...@@ -21,6 +21,33 @@ img { ...@@ -21,6 +21,33 @@ img {
font-weight: bold; font-weight: bold;
} }
.button {
border-radius: 8px;
display:block;
font-size: 16px;
font-weight:bold;
margin: 0 auto;
padding: 20px 0px 20px 0px;
text-align:center;
text-decoration:none;
width: 100%;
}
.button.main {
background-color: #222;
color: #FFF !important;
}
.button.secondary {
background-color: #CCC;
color: #222;
}
.button:hover {
background-color: cornflowerblue;
color: #FFF !important;
}
.post { .post {
background-color: #FFF; background-color: #FFF;
border-radius: 8px; border-radius: 8px;
...@@ -28,9 +55,7 @@ img { ...@@ -28,9 +55,7 @@ img {
} }
.posts { .posts {
margin: 0 auto;
padding-left: 0px; padding-left: 0px;
width: 80%;
} }
.posts li { .posts li {
...@@ -71,6 +96,23 @@ img { ...@@ -71,6 +96,23 @@ img {
.wrapper { .wrapper {
margin: 0 auto; margin: 0 auto;
padding: 32px 0px 32px 0px;
width: 80%; width: 80%;
} }
\ No newline at end of file
/* under wrappers */
.wrapper.detail {
padding: 32px 0px 32px 0px;
}
.wrapper.list {
padding: 0px 0px 32px 0px;
}
.wrapper form ul {
padding-left: 0px;
}
.wrapper form label {
font-weight:bold;
}
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