Commit 702d2c3d authored by Ray Rafael Abenido's avatar Ray Rafael Abenido

New Feature: feature to allow users to add new posts to the database partially...

New Feature: feature to allow users to add new posts to the database partially implemented. Image uploading still not working properly.
parent dcfbdfaa
{% extends 'forum/base.html' %}
{% block content %}
<p> Nice </p>
{% endblock %}
\ No newline at end of file
...@@ -14,4 +14,7 @@ ...@@ -14,4 +14,7 @@
</li> </li>
</ul> </ul>
{% endfor %} {% endfor %}
<br>
<p>Click here to create a new post: <a href="{% url 'Show Add Post' %}"> New Forum Post</a></p>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends 'forum/base.html' %} {% extends 'forum/base.html' %}
{% block content %} {% block content %}
<form action="confirmation" method="POST"> <h2> New Forum Post: </h2>
<form action="{% url 'Add New Post' %}" method="POST">
{% csrf_token %} {% csrf_token %}
<h3> Post Name: </h3> <input type="text" name="post_title"> <br>
<h3> Post Body: </h3> <input type="text" name="post_body"> <br> <!-- POST TITLE -->
<h3> Author ID </h3> <input type="text" name="author_id"> <br> <label for="post_title"> Post Name: </label> <br>
<input type="text" name="post_title" id="post_title"> <br>
<br>
<!-- POST BODY -->
<label for="post_body"> Post Body: </label> <br>
<textarea name="post_body" id="post_body" rows="10" cols="100">Type here!</textarea> <br>
<br>
<!-- AUTHOR LIST -->
<label for="author_list"> Author List: </label> <br>
<select name="post_author" id="author_list">
{% for user in users %}
<option value="{{user.id}}">{{user.first_name}} {{user.last_name}}</option>
{% endfor %}
</select> <br>
<br>
<!--IMAGE -->
<label for="post_image"> Upload Image: </label> <br>
<input type="file" name="post_image" id="post_image" accept="image/*"> <br>
<br>
<!--SUBMIT -->
<input type="submit"> <input type="submit">
</form> </form>
{% endblock %} {% endblock %}
...@@ -4,6 +4,8 @@ from . import views ...@@ -4,6 +4,8 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.show_index, name="Show Forum Page"), path('', views.show_index, name="Show Forum Page"),
path('post/<int:post_id>/details', views.show_post, name="Show Post Details"), path('post/<int:post_id>/details', views.show_post, name="Show Post Details"),
path('posts/add', views.show_form, name="Show Add Post"),
path('posts/add/success', views.add_new_post, name="Add New Post")
# Disabled for the time being. May be used again in the near future. # Disabled for the time being. May be used again in the near future.
......
# =================================
# MODULE IMPORTS
# =================================
# importing from django
from django.shortcuts import render from django.shortcuts import render
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
# importing from other apps in project
from .models import Post, Reply from .models import Post, Reply
from homepage.models import WidgetUser
# from python
import datetime
# Create your views here. # =================================
# FUNCTION LIST
# =================================
# for showing list of posts
def show_index(request): def show_index(request):
posts = Post.objects.all().order_by("-pub_date") posts = Post.objects.all().order_by("-pub_date")
return render(request, 'forum/index.html', {'posts':posts}) return render(request, 'forum/index.html', {'posts':posts})
# for showing post details given a post_id
def show_post(request, post_id): def show_post(request, post_id):
try: try:
post = Post.objects.get(pk=post_id) post = Post.objects.get(pk=post_id)
...@@ -16,11 +32,27 @@ def show_post(request, post_id): ...@@ -16,11 +32,27 @@ def show_post(request, post_id):
replies = Reply.objects.filter(post=post_id).order_by("-pub_date") replies = Reply.objects.filter(post=post_id).order_by("-pub_date")
return render(request, 'forum/post_details.html', {'post':post, 'replies': replies}) return render(request, 'forum/post_details.html', {'post':post, 'replies': replies})
# The following functions are not used for the time being. May be used again # for showing form where users can add new posts.
# in the near future.
def show_form(request): def show_form(request):
return render(request, 'forum/post_form.html', {}) users = WidgetUser.objects.all()
return render(request, 'forum/post_form.html', {'users':users})
# for adding a new post to database, then returning back to reply list
def add_new_post(request):
title = request.POST.get("post_title")
body = request.POST.get("post_body")
author = request.POST.get("post_author")
image = request.POST.get("post_image")
current_date = datetime.datetime.now()
new_post = Post.objects.create(post_title=title, post_body=body,author_id=author,
post_image=image,pub_date=current_date)
posts = Post.objects.all().order_by("-pub_date")
return render(request, 'forum/index.html', {'posts':posts})
# =================================
# UNUSED FUNCTIONS BUT NOT DEPRECATED
# =================================
def show_confirmation(request): def show_confirmation(request):
if (request.method == "POST"): if (request.method == "POST"):
......
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