Commit e44b4009 authored by Rurik Serzo's avatar Rurik Serzo

Added add post form

parent c6f0bfa1
No preview for this file type
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = ["post_title", "post_body", "author"]
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Forum{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'forum.css' %}">
{% endblock %}
{% block content %}
<h1>New Forum Post</h1>
<form action="{% url 'forum:new-post' %}" method="POST">
{% csrf_token %}
{{ post_form.as_p }}
<button class="button-style" type="submit">Save Post</button>
</form>
<button class="button-style"><a href="/forum">Back to Forum</a></button>
{% endblock %}
\ No newline at end of file
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<img src="{% static 'post1.jpg' %}" alt="Leni Robredo"> <img src="{% static 'post1.jpg' %}" alt="Leni Robredo">
{% elif post.id == 2 %} {% elif post.id == 2 %}
<img src="{% static 'post2.jpg' %}" alt="Lady Gaga MET camp"> <img src="{% static 'post2.jpg' %}" alt="Lady Gaga MET camp">
{% else %} {% elif post.id == 3 %}
<img src="{% static 'post3.jpg' %}" alt="Bruno Mars Grammys 2022"> <img src="{% static 'post3.jpg' %}" alt="Bruno Mars Grammys 2022">
{% endif %} {% endif %}
......
from django.urls import path from django.urls import path
from .views import index, PostDetailView from .views import index, PostDetailView, new_post
urlpatterns = [ urlpatterns = [
path("", index, name="index"), path("", index, name="index"),
path("<int:pk>/details", PostDetailView.as_view(), name="post-detail" ) path("<int:pk>/details", PostDetailView.as_view(), name="post-detail"),
path("add", new_post, name="new-post")
] ]
app_name = "Forum" app_name = "Forum"
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.template import loader from django.template import loader
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from .models import Post, Reply from .models import Post, Reply
from .forms import PostForm
def index(request): def index(request):
context = { context = {
...@@ -13,4 +14,14 @@ def index(request): ...@@ -13,4 +14,14 @@ def index(request):
return HttpResponse(template.render(context, request)) return HttpResponse(template.render(context, request))
class PostDetailView(DetailView): class PostDetailView(DetailView):
model = Post model = Post
\ No newline at end of file
def new_post(request):
if request.method == "POST":
post_form = PostForm(request.POST)
if post_form.is_valid():
post_form.save()
return redirect("forum:index")
else:
post_form = PostForm()
return render(request, 'forum/new_post.html', {"post_form": post_form})
\ No newline at end of file
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
border: 3px solid blue; border: 3px solid blue;
padding: 8px 20px; padding: 8px 20px;
margin-left: 30px; margin-left: 30px;
text-decoration: underline;
color: blue;
} }
.post-list { .post-list {
......
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