add new Forum post form, convert to CBV, minor QoL changes

parent e7a413b3
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['post_title', 'post_body', 'author']
\ No newline at end of file
body {
padding: 64px 160px;
}
#back {
margin: 0 0 32px;
display: flex;
align-items: center;
}
#back a {
color: black;
text-decoration: none;
margin-left: 8px;
}
#back a:hover {
text-decoration: underline;
}
form > * {
display: block;
}
form > label {
font-size: 1rem;
margin-bottom: 4px;
}
form > input,
form > select {
margin-bottom: 24px;
padding: 8px 16px;
border: 1px solid black;
border-radius: 4px;
font-size: 1rem;
}
#save-post-btn {
color: white;
font-size: 16px;
background-color: #187bcd;
display: flex;
align-items: center;
margin-top: 32px;
padding: 16px 32px;
border: none;
border-radius: 4px;
width: fit-content;
cursor: pointer;
}
\ No newline at end of file
...@@ -27,3 +27,23 @@ body { ...@@ -27,3 +27,23 @@ body {
.posts > li a:hover { .posts > li a:hover {
text-decoration: underline; text-decoration: underline;
} }
#add-post-btn {
color: white;
text-decoration: none;
font-weight: normal;
background-color: #187bcd;
display: flex;
align-items: center;
margin-top: 32px;
padding: 16px 32px;
border-radius: 4px;
width: fit-content;
}
#add-post-btn > svg {
margin-right: 16px;
}
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block styles %}
<link rel="stylesheet" type="text/css" href="{% static 'Forum/styles.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'Forum/add.css' %}">
{% endblock %}
{% block title %}Add Forum Post{% endblock %}
{% block content %}
<main>
<div id="back">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path
fill-rule="evenodd"
d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"
stroke="black"
/>
</svg>
<a href="/forum">Back to all posts</a>
</div>
<h1>New Forum Post</h1>
<form action="/posts/add/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" id="save-post-btn" value="Save Post">
</form>
</main>
{% endblock %}
\ No newline at end of file
...@@ -18,5 +18,20 @@ ...@@ -18,5 +18,20 @@
<li><a href="/posts/{{ post.pk }}/details"><strong>{{ post.post_title }}</strong> by <strong>{{ post.author.first_name }} {{ post.author.last_name }}</strong> dated <strong>{{ post.pub_date|date:"d/m/Y" }}</strong></a></li> <li><a href="/posts/{{ post.pk }}/details"><strong>{{ post.post_title }}</strong> by <strong>{{ post.author.first_name }} {{ post.author.last_name }}</strong> dated <strong>{{ post.pub_date|date:"d/m/Y" }}</strong></a></li>
{% endfor %} {% endfor %}
</ul> </ul>
<a href="/posts/add" id="add-post-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-plus-square" viewBox="0 0 16 16">
<path
d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h12zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z"
stroke="white"
stroke-width="0.5"
/>
<path
d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"
stroke="white"
stroke-width="0.5"
/>
</svg>
New Forum Post
</a>
</main> </main>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views import View
from .models import Post, Reply from .models import Post, Reply
from .forms import PostForm
def get_readable_time(timestamp): def get_readable_time(timestamp):
return timestamp.strftime("%m/%d/%Y") return timestamp.strftime("%m/%d/%Y")
...@@ -14,3 +16,13 @@ def details(request, post_id): ...@@ -14,3 +16,13 @@ def details(request, post_id):
except Post.DoesNotExist: except Post.DoesNotExist:
raise Http404('Post does not exist') raise Http404('Post does not exist')
return render(request, 'Forum/details.html', {'post': post, 'replies_sorted': post.reply_set.order_by('-pub_date')}) return render(request, 'Forum/details.html', {'post': post, 'replies_sorted': post.reply_set.order_by('-pub_date')})
class AddForumPostView(View):
def get(self, request):
return render(request, 'Forum/add.html', {'form': PostForm()})
def post(self, request):
form = PostForm(request.POST)
if form.is_valid():
new_post = form.save()
return redirect('/forum')
\ No newline at end of file
...@@ -23,6 +23,7 @@ from Homepage.views import UserPageView ...@@ -23,6 +23,7 @@ from Homepage.views import UserPageView
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('forum/', include('Forum.urls', namespace='Forum')), path('forum/', include('Forum.urls', namespace='Forum')),
path('posts/add/', Forum_views.AddForumPostView.as_view(), name='add_forum_post'),
path('posts/<int:post_id>/details/', Forum_views.details, name='post_details'), path('posts/<int:post_id>/details/', Forum_views.details, name='post_details'),
path('homepage/', include('Homepage.urls', namespace="Homepage")), path('homepage/', include('Homepage.urls', namespace="Homepage")),
path('users/<int:user_id>/details', UserPageView.as_view(), name='user-details'), path('users/<int:user_id>/details', UserPageView.as_view(), name='user-details'),
......
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