Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
widget_group3
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jonathan Talbot
widget_group3
Commits
1f43cf10
Commit
1f43cf10
authored
May 23, 2022
by
Jonathan Talbot
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'flores/forum' into 'main'
Flores/forum See merge request
!14
parents
79f420d2
3da5dd23
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
94 additions
and
37 deletions
+94
-37
forms.py
widget_group3/forum/forms.py
+7
-0
models.py
widget_group3/forum/models.py
+15
-12
post_detail.html
widget_group3/forum/templates/forum/post_detail.html
+14
-0
post_form.html
widget_group3/forum/templates/forum/post_form.html
+9
-0
post_list.html
widget_group3/forum/templates/forum/post_list.html
+16
-0
urls.py
widget_group3/forum/urls.py
+5
-2
views.py
widget_group3/forum/views.py
+26
-7
urls.py
widget_group3/widget_group3/urls.py
+2
-16
No files found.
widget_group3/forum/forms.py
0 → 100644
View file @
1f43cf10
from
django.forms
import
ModelForm
from
.models
import
Post
class
PostForm
(
ModelForm
):
class
Meta
:
model
=
Post
fields
=
[
'post_title'
,
'post_body'
,
'author'
]
# Selected fields for the form
\ No newline at end of file
widget_group3/forum/models.py
View file @
1f43cf10
from
django.db
import
models
from
django.urls
import
reverse
from
django.core.validators
import
RegexValidator
# Using WidgetUser from homepage
from
homepage.models
import
WidgetUser
class
Post
(
models
.
Model
):
post_title
=
models
.
CharField
(
max_length
=
5
0
)
post_body
=
models
.
CharField
(
max_length
=
1
00
)
post_title
=
models
.
CharField
(
'Title'
,
max_length
=
10
0
)
post_body
=
models
.
CharField
(
'Body'
,
max_length
=
10
00
)
pub_date
=
models
.
DateTimeField
(
auto_now_add
=
True
,
editable
=
False
)
author
=
models
.
ForeignKey
(
WidgetUser
,
on_delete
=
models
.
CASCADE
)
# author_name = WidgetUser.objects.get(fk=author).full_name()
# Takes URL to ForumDetailView
def
get_absolute_url
(
self
):
return
reverse
(
'Details'
,
args
=
[(
self
.
pk
)])
def
__str__
(
self
):
return
'{} by {} dated {}:
\n
{}'
.
format
(
self
.
post_title
,
self
.
author
.
full_name
(),
self
.
pub_date
,
self
.
post_body
)
def
get_author_name
(
self
):
return
str
(
self
.
author
.
first_name
+
" "
+
self
.
author
.
last_name
)
def
get_absolute_url
(
self
):
return
reverse
(
'forum'
,
args
=
[(
self
.
post_title
)])
author_name
=
get_author_name
class
Reply
(
models
.
Model
):
reply_body
=
models
.
CharField
(
max_length
=
1
00
)
reply_body
=
models
.
CharField
(
max_length
=
5
00
)
pub_date
=
models
.
DateTimeField
(
auto_now_add
=
True
,
editable
=
False
)
author
=
models
.
ForeignKey
(
WidgetUser
,
on_delete
=
models
.
CASCADE
)
replied_post
=
models
.
ForeignKey
(
Post
,
on_delete
=
models
.
CASCADE
)
replied_post
=
models
.
ForeignKey
(
Post
,
related_name
=
'replies'
,
on_delete
=
models
.
CASCADE
)
def
get_author_name
(
self
):
return
str
(
self
.
author
.
first_name
+
" "
+
self
.
author
.
last_name
)
def
__str__
(
self
):
return
'
\n
Reply by {} dated {}:
\n
{}'
.
format
(
self
.
author
.
full_name
(),
self
.
pub_date
,
self
.
reply_body
)
\ No newline at end of file
author_name
=
get_author_name
\ No newline at end of file
widget_group3/forum/templates/forum/post_detail.html
0 → 100644
View file @
1f43cf10
{% block content %}
<h3>
{{post.post_title}}
</h3>
<p>
by {{post.author_name}}, {{post.pub_date|date:"d/m/Y"}}
<p>
{{post.post_body}}
<ul>
{% for reply in post.replies.all %}
<li>
{{reply.author_name}}, {{reply.pub_date|date:"d/m/Y"}}:
<br>
{{reply.reply_body}}
</li>
{% endfor %}
</ul>
<a
href=
"{% url 'Forum' %}"
>
Back to Forum
</a>
{% endblock %}
\ No newline at end of file
widget_group3/forum/templates/forum/post_form.html
0 → 100644
View file @
1f43cf10
{% block content %}
<h3>
New Forum Post
</h3>
<form
action=
"{% url 'Add' %}"
method=
"POST"
>
{% csrf_token %}
{{ form.as_p }}
<input
type=
"submit"
value=
"Save Post"
>
</form>
<a
href=
"{% url 'Forum' %}"
>
Back to Forum
</a>
{% endblock %}
\ No newline at end of file
widget_group3/forum/templates/forum/post_list.html
0 → 100644
View file @
1f43cf10
{% block content %}
{% block title %}Welcome to Widget’s Forum!{% endblock %}
<p>
Forum posts:
</p>
{% if post_list %}
<ul>
{% for post in post_list %}
<li>
<a
href=
"{{ post.get_absolute_url }}"
>
{{ post.post_title }}
</a>
by {{post.author_name}} dated {{post.pub_date|date:"d/m/Y"}}
<br>
</li>
{% endfor %}
</ul>
{% else %}
<p>
There are no posts.
</p>
{% endif %}
<p><a
href=
"{% url 'Add' %}"
>
New Forum Post
</a></p>
{% endblock %}
\ No newline at end of file
widget_group3/forum/urls.py
View file @
1f43cf10
from
django.urls
import
path
from
.views
import
forum
from
.
import
views
from
.views
import
ForumListView
,
ForumDetailView
urlpatterns
=
[
path
(
''
,
forum
,
name
=
'forum'
)
path
(
''
,
ForumListView
.
as_view
(),
name
=
'Forum'
),
path
(
'<int:pk>/details/'
,
ForumDetailView
.
as_view
(),
name
=
'Details'
),
path
(
'add/'
,
views
.
addPost
,
name
=
'Add'
),
]
\ No newline at end of file
widget_group3/forum/views.py
View file @
1f43cf10
from
django.http
import
HttpResponse
from
.models
import
Post
,
Reply
from
django.views.generic
import
ListView
,
DetailView
from
django.shortcuts
import
render
,
redirect
from
django.db
import
models
from
.models
import
Post
from
.forms
import
PostForm
def
forum
(
request
):
post
=
Post
.
objects
.
all
()
reply
=
Reply
.
objects
.
all
()
output
=
"FORUM POSTS:
\n
"
+
"
\n
"
.
join
([
str
(
x
)
for
x
in
post
])
+
"
\n
"
.
join
([
str
(
z
)
for
z
in
reply
])
return
HttpResponse
(
output
,
content_type
=
"text/plain"
)
class
ForumListView
(
ListView
):
queryset
=
Post
.
objects
.
order_by
(
'-pub_date'
)
# Orders publication by most recent
context_object_name
=
"post_list"
template_name
=
"post_list.html"
class
ForumDetailView
(
DetailView
):
model
=
Post
template_name
=
"forum/post_detail.html"
def
addPost
(
request
):
form
=
PostForm
()
if
request
.
method
==
'POST'
:
form
=
PostForm
(
request
.
POST
)
if
form
.
is_valid
():
form
.
pub_date
=
models
.
DateTimeField
(
auto_now_add
=
True
,
editable
=
False
)
# Sets current time as pub_date
new_post
=
form
.
save
()
# Creates new post
return
redirect
(
'Details'
,
pk
=
new_post
.
pk
)
# Redirects to detailed view of new post
else
:
form
=
PostForm
()
context
=
{
'form'
:
form
}
return
render
(
request
,
'forum/post_form.html'
,
context
)
# Takes post_form template and displays form and post button
\ No newline at end of file
widget_group3/widget_group3/urls.py
View file @
1f43cf10
"""widget_group3 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from
django.contrib
import
admin
from
django.urls
import
path
,
include
...
...
@@ -23,6 +8,7 @@ urlpatterns = [
path
(
'homepage/'
,
include
(
'homepage.urls'
),
name
=
'Homepage'
),
path
(
'users/'
,
include
(
'homepage.urls'
),
name
=
'Homepage'
),
path
(
'forum/'
,
include
(
'forum.urls'
),
name
=
'Forum'
),
path
(
'posts/'
,
include
(
'forum.urls'
),
name
=
'Forum'
),
path
(
'assignments/'
,
include
(
'assignments.urls'
),
name
=
'Assignments'
),
path
(
'announcements/'
,
include
(
'announcements.urls'
),
name
=
'Announcement Board'
),
]
]
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment