Commit 63929e03 authored by Christian Sarabia's avatar Christian Sarabia

post_title = models.CharField(max_length=50)

    post_body = models.TextField(max_length=1500)
    pub_date = models.DateTimeField("date published", auto_now_add=True)
    author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
    post_image
Updated the Forum to add forms and views
parent 1bb71408
from django import forms
from homepage.models import WidgetUser
from .models import Post
class AddForm(forms.ModelForm):
class Meta:
model = Post
fields = "__all__"
# post_title = forms.CharField(label='Title', max_length=50)
# post_body = forms.CharField(label='Body', max_length=1500, widget=forms.Textarea)
# # pub_date = models.DateTimeField("date published")
# author = forms.ModelChoiceField(label='Author', queryset=WidgetUser.objects.all())
# post_image = forms.ImageField(required=False)
\ No newline at end of file
...@@ -5,7 +5,7 @@ from homepage.models import WidgetUser ...@@ -5,7 +5,7 @@ from homepage.models import WidgetUser
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.TextField(max_length=1500) post_body = models.TextField(max_length=1500)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published", auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE)
post_image = models.ImageField(null = True, blank = True, upload_to = "forum/") post_image = models.ImageField(null = True, blank = True, upload_to = "forum/")
......
{% extends 'base.html' %}
{% load static %}
{% block styles %}
<link rel="stylesheet" type="text/css" href="{% static 'forum/style.css' %}">
{% endblock %}
{% block title %}New Forum Post{% endblock %}
{% block content %}
<h1>New Forum Post</h1>
<div>
<form action="add" method="post">
{% csrf_token %}
<ul>
<li>Title: {{ form.post_title }}</li>
<li>Body: {{ form.post_body }}</li>
<li>Author: {{ form.author }}</li>
<li>Image: {{ form.post_image }}</li>
</ul>
<input type="submit" value="Save Post" style="float:right;font-size:100%">
</form>
</div>
{% endblock %}
\ No newline at end of file
...@@ -10,14 +10,15 @@ ...@@ -10,14 +10,15 @@
<h1>Forum</h1> <h1>Forum</h1>
<div> <div>
<h2>Forum posts:</h2> <h2>Forum posts:</h2>
{% if posts %}
<ul> <ul>
{% if posts %}
{% for post in posts %} {% for post in posts %}
<li><a href="{% url 'forum:details' post.id%}">{{post.post_title}}</a> by {{post.author.first_name}} {{post.author.last_name}} dated {{post.pub_date|date:"d/m/Y"}}</li> <li><a href="{% url 'forum:details' post.id%}">{{post.post_title}}</a> by {{post.author.first_name}} {{post.author.last_name}} dated {{post.pub_date|date:"d/m/Y"}}</li>
{% endfor %} {% endfor %}
</ul>
{% else %} {% else %}
<p>There is no post.</p> <p>There is no post.</p>
{% endif %} {% endif %}
<li><a href="/forum/add">Write a new forum post...</a></li>
</ul>
</div> </div>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -4,5 +4,6 @@ from . import views ...@@ -4,5 +4,6 @@ from . import views
app_name = "forum" app_name = "forum"
urlpatterns = [ urlpatterns = [
path('', views.index, name="indexForum"), path('', views.index, name="indexForum"),
path('<int:post_id>/details', views.details, name="details") path('<int:post_id>/details', views.details, name="details"),
path('add', views.AddView.as_view(), name="add")
] ]
from django.http import Http404
from .models import Post, Reply from .models import Post, Reply
from .forms import AddForm
from django.http import Http404
from django.shortcuts import render from django.shortcuts import render
from django.views import View
# Create your views here.
def index(request): def index(request):
posts = Post.objects.order_by("-pub_date") posts = Post.objects.order_by("-pub_date")
context = { context = {
...@@ -10,20 +11,6 @@ def index(request): ...@@ -10,20 +11,6 @@ def index(request):
} }
return render(request, "forum/index.html", context) return render(request, "forum/index.html", context)
# display_output = "<u><b>FORUM POSTS</u></b>:<br>"
# for post in Post.objects.all():
# display_output += f"<b>{post.post_title} by {post.author.first_name} {post.author.last_name}</b> dated {str(post.pub_date)}:\
# <br>{post.post_body}"
# replies = Reply.objects.filter(post=post)
# for reply in replies:
# display_output += f"<br><b>Reply by {reply.author.first_name} {reply.author.last_name}</b> dated {reply.pub_date}:\
# <br>{reply.reply_body}"
# display_output += "<br><br>"
# return HttpResponse(display_output)
def details(request, post_id): def details(request, post_id):
try: try:
post = Post.objects.get(pk=post_id) post = Post.objects.get(pk=post_id)
...@@ -36,3 +23,21 @@ def details(request, post_id): ...@@ -36,3 +23,21 @@ def details(request, post_id):
} }
return render(request, "forum/detail.html", context) return render(request, "forum/detail.html", context)
class AddView(View):
def get(self, request):
form = AddForm()
context = {'form' : form}
return render(request, 'forum/add.html', context)
def post(self, request):
form = AddForm(request.POST)
if form.is_valid():
print(form.cleaned_data['post_title'])
form.save()
context = {'form' : form}
return render(request, 'forum/add.html', context)
...@@ -7,12 +7,6 @@ ...@@ -7,12 +7,6 @@
{% block title %}Homepage{% endblock %} {% block title %}Homepage{% endblock %}
{% block content %} {% block content %}
<nav class="topnav">
<a href="{% url 'homepage:indexHomepage' %}">Homepage</a>
<a href="{% url 'announcements:indexAnnouncements' %}">Announcements</a>
<a href="{% url 'forum:indexForum' %}">Forum</a>
<a href="{% url 'assignments:indexAssignments' %}">Assignments</a>
</nav>
<h1 class="welcome">Welcome to Widget!</h1> <h1 class="welcome">Welcome to Widget!</h1>
<div class="users"> <div class="users">
......
.topnav {
padding: 30px 30px;
}
.topnav a {
display: inline-block;
padding: 8px;
color:#131111;
text-align: center;
text-decoration: none;
}
.topnav a:hover {
text-decoration: underline;
}
\ No newline at end of file
...@@ -6,11 +6,20 @@ ...@@ -6,11 +6,20 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'base.css' %}">
{% block styles %} {% block styles %}
{% endblock %} {% endblock %}
<title>{% block title %} Alipins {% endblock %}</title> <title>{% block title %} Alipins {% endblock %}</title>
</head> </head>
<body> <body>
<nav class="topnav">
<a href="{% url 'homepage:indexHomepage' %}">Homepage</a>
<a href="{% url 'announcements:indexAnnouncements' %}">Announcements</a>
<a href="{% url 'forum:indexForum' %}">Forum</a>
<a href="{% url 'assignments:indexAssignments' %}">Assignments</a>
</nav>
{% block content %} {% block content %}
{% endblock %} {% endblock %}
</body> </body>
......
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