Commit 592ac8f6 authored by N1o9r4b7e9r6to's avatar N1o9r4b7e9r6to

did requirements for lab 3, can now make post on /forum

parent 732ef375
from django.contrib import admin from django.contrib import admin
from django.forms import TextInput, Textarea
from .models import Post, Reply from .models import Post, Reply
from django.db import models
# Register your models here. # Register your models here.
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
formfield_overrides = {
models.CharField: {'widget':TextInput(attrs={'size':'20'})},
models.TextField: {'widget':Textarea(attrs={'rows':'4','cols':'40'})},
}
model = Post model = Post
list_display=('post_title','pub_date','author') list_display=('post_title','pub_date','author')
list_filter=('pub_date','post_title','author') list_filter=('pub_date','post_title','author')
admin.site.register(Post, PostAdmin) admin.site.register(Post, PostAdmin)
......
from django import forms
from .models import Post
class addPostsForm(forms.ModelForm):
post_title = forms.CharField(label='Title:')
post_body = forms.CharField(label="Post",widget=forms.Textarea(attrs={'rows':'20','cols':'150'}))
class Meta:
model = Post
fields = '__all__'
# Generated by Django 4.0.3 on 2022-05-21 07:43
from django.db import migrations, models
import forum.models
class Migration(migrations.Migration):
dependencies = [
('forum', '0007_remove_reply_active_alter_post_author_and_more'),
]
operations = [
migrations.AddField(
model_name='post',
name='photo',
field=models.ImageField(blank=True, default=None, null=True, upload_to=forum.models.path_and_rename),
),
]
from django.db import models from django.db import models
from homepage.models import WidgetUser from homepage.models import WidgetUser
import os
import datetime import datetime
def path_and_rename(instance,filename):
upload_to="forum\\static\\images"
ext='png'
if instance.pk:
filename='{}.{}'.format(instance.pk,ext)
#print(os.path.join(upload_to,filename))
return os.path.join(upload_to,filename)
#class date(models.Model): #class date(models.Model):
#datenow = datetime.now().date() #datenow = datetime.now().date()
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=100000)
pub_date = models.DateField(auto_now_add=True) pub_date = models.DateField(auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default = None) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default = None)
post_body = models.CharField(max_length=100000)
photo=models.ImageField(upload_to=path_and_rename,null=True,blank=True, default=None)
def __str__(self): def __str__(self):
return'{}: {}'.format(self.post_title,self.pub_date) return'{}: {}'.format(self.post_title,self.pub_date)
......
...@@ -86,3 +86,33 @@ h1 { ...@@ -86,3 +86,33 @@ h1 {
font-weight: bold; font-weight: bold;
font-family: "Gill Sans", sans-serif; font-family: "Gill Sans", sans-serif;
} }
a:link {
color: green;
font-size: 20px;
font-weight: bold;
font-family: "Gill Sans", sans-serif;
}
a:visited {
color: blueviolet;
font-size: 20px;
font-weight: bold;
font-family: "Gill Sans", sans-serif;
}
a:hover {
color: red;
text-decoration: underline;
font-size: 20px;
font-weight: bold;
font-family: "Gill Sans", sans-serif;
}
a:active {
color: black;
text-decoration: underline;
font-size: 20px;
font-weight: bold;
font-family: "Gill Sans", sans-serif;
}
\ No newline at end of file
{% extends "forum/base.html" %}
{% block content %}
<center>New Forum Post</center>
<a href = "/forum/" style="background-color:whitesmoke;border:2px solid black;">Back to main Forum</a>
<form action="{% url 'forum:addForumPosts' %}" method="POST">
{% csrf_token %}
<center>Title:{{addposts.post_title}} Author: {{addposts.author}} {{addposts.pub_date}}</center>
<br>
<br>
Post Body:
<center style="background-color: aquamarine;">{{addposts.post_body}}</center>
<br>
<button class="button" type="submit">Save Post</button>
{{addposts.photo}}
</form>
<br>
{% endblock %}
\ No newline at end of file
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
dated {{posts.pub_date|date:'d/m/Y'}}</li></ul> dated {{posts.pub_date|date:'d/m/Y'}}</li></ul>
{% endfor %} {% endfor %}
</h4> </h4>
<form action="/posts/add/">
<input type="submit" value="New Forum Post"/>
</form>
<left><img src="../../static/images/twopeopletalking.jpg" alt="people talking" style="width:450px;float:left;"></left> <left><img src="../../static/images/twopeopletalking.jpg" alt="people talking" style="width:450px;float:left;"></left>
<center><img src="../../static/images/forum.PNG" align="left" alt="people bubbes" style="width:450px;;margin-left:20px;"></center> <center><img src="../../static/images/forum.PNG" align="left" alt="people bubbes" style="width:450px;;margin-left:20px;"></center>
<right><img src="../../static/images/teachingstudents.jpg" alt="teacher teaching" style="width:450px;float:right;"> <right><img src="../../static/images/teachingstudents.jpg" alt="teacher teaching" style="width:450px;float:right;">
......
from django.urls import path from django.urls import path
from .views import forumposts, DisplayPostdetails from .views import forumposts, DisplayPostdetails, addForumPosts
urlpatterns = [ urlpatterns = [
path('forum/', forumposts, name="post"), path('forum/', forumposts.as_view(), name="post"),
path('posts/<int:post_id>/details', DisplayPostdetails, name='DisplayPostdetails'), path('posts/<int:post_id>/details', DisplayPostdetails, name='DisplayPostdetails'),
path('posts/add/',addForumPosts,name="addForumPosts")
] ]
app_name='forum' app_name='forum'
...@@ -5,8 +5,13 @@ from django.http import HttpResponse ...@@ -5,8 +5,13 @@ from django.http import HttpResponse
from django.template import Template, Context from django.template import Template, Context
from django.template.loader import get_template from django.template.loader import get_template
from forum.models import Post, Reply from forum.models import Post, Reply
from .forms import addPostsForm
from django.views import View
from django.shortcuts import render, redirect
from django.views.generic.list import ListView
def forumposts(request): class forumposts(View):
def get(self, request):
all_posts = models.Post.objects.order_by("-id") all_posts = models.Post.objects.order_by("-id")
context = { context = {
"all_posts":all_posts, "all_posts":all_posts,
...@@ -26,3 +31,14 @@ def DisplayPostdetails(request, post_id): ...@@ -26,3 +31,14 @@ def DisplayPostdetails(request, post_id):
"img": "images/" + str(post_id) + ".png" "img": "images/" + str(post_id) + ".png"
} }
return render(request, "forum/details.html", context) return render(request, "forum/details.html", context)
def addForumPosts(request):
if request.method == 'POST':
form = addPostsForm(request.POST)
if form.is_valid():
new_post = form.save()
return redirect("forum:addForumPosts")
else:
form = addPostsForm()
return render(request, "forum/addforumposts.html",{"addposts":form})
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