Commit 3670b489 authored by Ron Rodillas's avatar Ron Rodillas

created add forum page and fixed issues with models

parent cfffc4c0
...@@ -22,5 +22,8 @@ class WidgetUser(models.Model): ...@@ -22,5 +22,8 @@ class WidgetUser(models.Model):
return self.department.home_unit return self.department.home_unit
def __str__(self): def __str__(self):
return self.last_name+ ", "+ self.first_name
def display_text(self):
return '{}, {} {}: {}, {}'.format(self.last_name, self.first_name, self.middle_name, self.dept_name, self.home_unit) return '{}, {} {}: {}, {}'.format(self.last_name, self.first_name, self.middle_name, self.dept_name, self.home_unit)
\ No newline at end of file
...@@ -8,7 +8,7 @@ from .models import WidgetUser ...@@ -8,7 +8,7 @@ from .models import WidgetUser
def index(request): def index(request):
return_string = '<body>Welcome to Widget!<br><br>WIDGET USERS:<br><ul>' return_string = '<body>Welcome to Widget!<br><br>WIDGET USERS:<br><ul>'
for user in WidgetUser.objects.all(): for user in WidgetUser.objects.all():
user_string = '<li>{}</li>'.format(user.__str__()) user_string = '<li>{}</li>'.format(user.display_text())
return_string += user_string return_string += user_string
return_string += '</ul></body>' return_string += '</ul></body>'
......
No preview for this file type
# Generated by Django 4.1.7 on 2023-05-13 13:56
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0001_initial'),
('forum', '0003_rename_reply_to_reply_forum_post'),
]
operations = [
migrations.AlterField(
model_name='forumpost',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='dashboard.widgetuser'),
),
]
# Generated by Django 4.1.7 on 2023-05-13 14:07
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0001_initial'),
('forum', '0004_alter_forumpost_author'),
]
operations = [
migrations.AlterField(
model_name='reply',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='dashboard.widgetuser'),
),
]
...@@ -4,7 +4,7 @@ from django.db import models ...@@ -4,7 +4,7 @@ from django.db import models
class ForumPost (models.Model): class ForumPost (models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
body = models.TextField() body = models.TextField()
author = models.CharField(max_length=100) author = models.ForeignKey('dashboard.WidgetUser', on_delete=models.CASCADE, null=True)
pub_datetime = models.DateTimeField(auto_now_add=True) pub_datetime = models.DateTimeField(auto_now_add=True)
def __str__(self): def __str__(self):
...@@ -16,12 +16,15 @@ class ForumPost (models.Model): ...@@ -16,12 +16,15 @@ class ForumPost (models.Model):
def get_name(self): def get_name(self):
return self.title return self.title
def get_author(self): def get_author_first_name(self):
return self.author return self.author.first_name
def get_author_last_name(self):
return self.author.last_name
class Reply (models.Model): class Reply (models.Model):
body = models.TextField() body = models.TextField()
author = models.CharField(max_length=100) author = models.ForeignKey('dashboard.WidgetUser', on_delete=models.CASCADE, null=True)
pub_datetime = models.DateTimeField(auto_now_add=True) pub_datetime = models.DateTimeField(auto_now_add=True)
forum_post = models.ForeignKey(ForumPost, on_delete=models.CASCADE) forum_post = models.ForeignKey(ForumPost, on_delete=models.CASCADE)
......
...@@ -14,10 +14,10 @@ Widget's Forum ...@@ -14,10 +14,10 @@ Widget's Forum
<div> <div>
{% for post in posts %} {% for post in posts %}
"{{post.get_name}}" by {{post.get_author}}<br> "{{post.get_name}}" by {{post.get_author_first_name}} {{post.get_author_last_name}}<br>
{% endfor %} {% endfor %}
<br><br><br> <br><br><br>
<button>New Post</button> <a href = "{% url 'forum:addPost' %}"><button>New Post</button></a>
<br><br> <br><br>
<a href="{% url 'dashboard:index' %}"> Dashboard</a> <a href="{% url 'dashboard:index' %}"> Dashboard</a>
<br> <br>
......
{% extends 'base.html' %}
{% load static %}
{% block title %}
{% endblock %}
{% block styles %}
{% endblock %}
{% block content %}
<h1> Add a new post:</h1><br>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save New Post">
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}
{% endblock %}
{% block styles %}
{% endblock %}
{% block content %}
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import *
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', index, name='index'),
path('forumposts/add/', PostCreateView.as_view(), name='addPost'),
] ]
app_name="forum" app_name="forum"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import ForumPost, Reply from .models import ForumPost, Reply
from django.views.generic.edit import CreateView, UpdateView
# Create your views here. # Create your views here.
def index(request): def index(request):
post_list = reversed(ForumPost.objects.all()) post_list = reversed(ForumPost.objects.all())
return render(request,"widget_group3/forum.html",{"posts":post_list}) return render(request,"forum/forum.html",{"posts":post_list})
class PostCreateView(CreateView):
model = ForumPost
fields = '__all__'
template_name = 'forum/forumpost-add.html'
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