Skip to content

Commit 343b407

Browse files
committedFeb 22, 2022
added pagination, markdown, humanize added my_account, pagination html files and also updated url, views.py and many more
1 parent 2d93704 commit 343b407

19 files changed

+277
-36
lines changed
 
767 Bytes
Binary file not shown.

‎accounts/views.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
from django.contrib.auth.forms import UserCreationForm
44
from django.contrib.auth import login as auth_login_user
55
from .forms import SignUpForm
6+
from django.views.generic import UpdateView
7+
from django.utils.decorators import method_decorator
8+
from django.contrib.auth.decorators import login_required
9+
from django.urls import reverse_lazy
10+
from django.contrib.auth.models import User
611

712
def signup(request):
813
if request.method == 'POST':
@@ -16,4 +21,16 @@ def signup(request):
1621
#form = UserCreationForm()
1722
form = SignUpForm()
1823

19-
return render(request, 'signup.html', {'form':form})
24+
return render(request, 'signup.html', {'form':form})
25+
26+
#CBV -for my account
27+
28+
@method_decorator(login_required, name='dispatch')
29+
class UserUpdateView(UpdateView):
30+
model = User
31+
fields = ('first_name', 'last_name', 'email')
32+
template_name = 'my_account.html'
33+
success_url = reverse_lazy('my_account')
34+
35+
def get_object(self):
36+
return self.request.user

‎db.sqlite3

4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎myFirstDjangoProject/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'django.contrib.sessions',
3939
'django.contrib.messages',
4040
'django.contrib.staticfiles',
41+
'django.contrib.humanize',
4142

4243
'widget_tweaks',
4344
'accounts',

‎myFirstDjangoProject/urls.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
urlpatterns = [
2828
path('', views.home, name='home'),#default home page path
2929
path('home/', views.home, name='home'),
30+
31+
#classbased view url
32+
#re_path(r'^home/$',views.BoardListView.as_view(), name='home'),
33+
3034
re_path(r'^signup/$',accounts_views.signup, name='signup'),
3135
re_path(r'^login/$',auth_views.LoginView.as_view(template_name='login.html'), name='login'),
3236
re_path(r'^logout/$',auth_views.LogoutView.as_view(),name='logout'),
@@ -55,6 +59,9 @@
5559
auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html'),
5660
name='password_reset_complete'),
5761

62+
re_path(r'^setting/account/$',accounts_views.UserUpdateView.as_view(),
63+
name='my_account'),
64+
5865
#user changed password url
5966
re_path(r'^settings/password/$',
6067
auth_views.PasswordChangeView.as_view(template_name='password_change.html'),
@@ -63,16 +70,27 @@
6370
auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'),
6471
name='password_change_done'),
6572

66-
re_path(r'^board_topic/(?P<pk>\d+)/$', views.board_topic, name='board_topic'),
73+
#FBV url board_topic
74+
#re_path(r'^board_topic/(?P<pk>\d+)/$', views.board_topic, name='board_topic'),
75+
#CBV-url board_topic
76+
re_path(r'^board_topic/(?P<pk>\d+)/$', views.TopicListView.as_view(),
77+
name='board_topic'),
78+
6779
re_path(r'^board_topic/(?P<pk>\d+)/new/$', views.new_board_topic, name='new_board_topic'),
6880

6981
#both url path will work
70-
re_path(r'^board_topic/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/$',
71-
views.topic_posts, name='topic_posts'),
82+
#FBV url topic_posts
83+
# re_path(r'^board_topic/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/$',
84+
# views.topic_posts, name='topic_posts'),
7285

7386
# path(r'^board_topic/int:pk/topics/int:topic_pk/',
7487
# views.topic_posts, name='topic_posts'),
7588

89+
#CBV url topic_posts
90+
re_path(r'^board_topic/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/$',
91+
views.PostListView.as_view(), name='topic_posts'),
92+
93+
7694
re_path(r'^board_topic/(?P<pk>\d+)/topics/(?P<topic_pk>\d+)/reply/$',
7795
views.reply_topic, name='reply_topic'),
7896

‎templates/base.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{{ user.username }}
2424
</a>
2525
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="userMenu">
26-
<a class="dropdown-item" href="#">My account</a>
26+
<a class="dropdown-item" href="{% url 'my_account' %}">My account</a>
2727
<a class="dropdown-item" href="{% url 'password_change' %}">Change password</a>
2828
<div class="dropdown-divider"></div>
2929
<a class="dropdown-item" href="{% url 'logout' %}">Log out</a>
@@ -51,6 +51,7 @@
5151
<script src="{% static 'js/jquery-3.6.0.min.js' %}"></script>
5252
<script src="{% static 'js/popper.min.js' %}"></script>
5353
<script src="{% static 'js/bootstrap.min.js' %}"></script>
54+
{% block javascript %}{% endblock %}
5455
</body>
5556
</html>
5657

‎templates/chat_board_topics.html

+53-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends 'base.html' %}
22

3+
{% load humanize %}
4+
35
{% block title %}
46
{{ board.name }} - {{ block.super }}
57
{% endblock %}
@@ -16,7 +18,7 @@
1618
<a href="{% url 'new_board_topic' chat_board.pk %}" class="btn btn-primary">New topic</a>
1719
</div>
1820

19-
<table class="table">
21+
<table class="table mb-4">
2022
<thead class="thead-inverse">
2123
<tr>
2224
<th>Topic</th>
@@ -33,13 +35,59 @@
3335
<td>{{ topic.boardStarter.username }}</td>
3436
<td>{{ topic.replies }}</td>
3537
<td>{{ topic.views }}</td>
36-
<td>{{ topic.lastUpdate }}</td>
38+
<td>{{ topic.lastUpdate|naturaltime }}</td>
3739
</tr>
3840
{% endfor %}
3941
</tbody>
4042
</table>
4143

42-
{% if topics.has_other_pages %}
44+
{% include 'includes/pagination.html' %}
45+
46+
<!--below code for CBV-->
47+
<!--{% if is_paginated %}
48+
<nav aria-label="Topics pagination" class="mb-4">
49+
<ul class="pagination">
50+
{% if page_obj.has_previous %}
51+
<li class="page-item">
52+
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
53+
</li>
54+
{% else %}
55+
<li class="page-item disabled">
56+
<span class="page-link">Previous</span>
57+
</li>
58+
{% endif %}
59+
60+
{% for page_num in paginator.page_range %}
61+
{% if page_obj.number == page_num %}
62+
<li class="page-item active">
63+
<span class="page-link">
64+
{{ page_num }}
65+
<span class="sr-only">(current)</span>
66+
</span>
67+
</li>
68+
{% else %}
69+
<li class="page-item">
70+
<a class="page-link" href="?page={{ page_num }}">{{ page_num }}</a>
71+
</li>
72+
{% endif %}
73+
{% endfor %}
74+
75+
{% if page_obj.has_next %}
76+
<li class="page-item">
77+
<a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
78+
</li>
79+
{% else %}
80+
<li class="page-item disabled">
81+
<span class="page-link">Next</span>
82+
</li>
83+
{% endif %}
84+
</ul>
85+
</nav>
86+
{% endif %}-->
87+
88+
89+
<!--below code for FBV-->
90+
<!--{% if topics.has_other_pages %}
4391
<nav aria-label="Topics pagination" class="mb-4">
4492
<ul class="pagination">
4593
{% if topics.has_previous %}
@@ -78,5 +126,6 @@
78126
{% endif %}
79127
</ul>
80128
</nav>
81-
{% endif %}
129+
{% endif %}-->
130+
82131
{% endblock %}

‎templates/edit_post.html

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
{% extends 'base.html'%}
22

3-
{% block title %}Edit post{% endblock %}
43
{% load static %}
4+
5+
{% block title %}Edit post{% endblock %}
6+
7+
8+
{% block stylesheet %}
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
10+
{% endblock %}
11+
12+
{% block javascript %}
13+
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
14+
<script>
15+
var simplemde = new SimpleMDE();
16+
</script>
17+
{% endblock %}
18+
19+
520
{% block breadcrumb %}
621
<li class="breadcrumb-item"><a href="{% url 'home' %}">Web Chat Boards</a></li>
722
<li class="breadcrumb-item"><a href="{% url 'board_topic' post.topic.boardName.pk %}">{{ post.topic.boardName.name }}</a></li>

‎templates/includes/pagination.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{% if is_paginated %}
2+
<nav aria-label="Topics pagination" class="mb-4">
3+
<ul class="pagination">
4+
{% if page_obj.has_previous %}
5+
<li class="page-item">
6+
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
7+
</li>
8+
{% else %}
9+
<li class="page-item disabled">
10+
<span class="page-link">Previous</span>
11+
</li>
12+
{% endif %}
13+
14+
{% for page_num in paginator.page_range %}
15+
{% if page_obj.number == page_num %}
16+
<li class="page-item active">
17+
<span class="page-link">
18+
{{ page_num }}
19+
<span class="sr-only">(current)</span>
20+
</span>
21+
</li>
22+
{% else %}
23+
<li class="page-item">
24+
<a class="page-link" href="?page={{ page_num }}">{{ page_num }}</a>
25+
</li>
26+
{% endif %}
27+
{% endfor %}
28+
29+
{% if page_obj.has_next %}
30+
<li class="page-item">
31+
<a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
32+
</li>
33+
{% else %}
34+
<li class="page-item disabled">
35+
<span class="page-link">Next</span>
36+
</li>
37+
{% endif %}
38+
</ul>
39+
</nav>
40+
{% endif %}

‎templates/my_account.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends 'base.html'%}
2+
{% block title %}
3+
My account
4+
{% endblock %}
5+
6+
{% block breadcrumb %}
7+
<li class="breadcrumb-item active">My account</li>
8+
{% endblock %}
9+
10+
{% block content %}
11+
<div class="row">
12+
<div class="col-lg-6 col-md-8 col-sm-10">
13+
<form method="post" novalidate>
14+
{% csrf_token %}
15+
{% include 'includes/form.html' %}
16+
<button type="submit" class="btn btn-success">Save changes</button>
17+
</form>
18+
</div>
19+
</div>
20+
{% endblock %}

‎templates/new_board_topic.html

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends 'base.html' %}
22

3+
{% load humanize %}
4+
35
{% block title %}
46
Start a New Chat Topic
57
{% endblock %}

‎templates/reply_topic.html

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
{% block title %}Post a reply{% endblock %}
66

7+
8+
{% block stylesheet %}
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
10+
{% endblock %}
11+
12+
{% block javascript %}
13+
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
14+
<script>
15+
var simplemde = new SimpleMDE();
16+
</script>
17+
{% endblock %}
18+
19+
720
{% block breadcrumb %}
821
<li class="breadcrumb-item"><a href="{% url 'home' %}">Web Chat Boards</a></li>
922
<li class="breadcrumb-item"><a href="{% url 'board_topic' topic.boardName.pk %}">{{ topic.boardName.name }}</a></li>
@@ -13,7 +26,7 @@
1326

1427
{% block content %}
1528

16-
<form method="post" class="mb-4">
29+
<form method="post" class="mb-4" novalidate>
1730
{% csrf_token %}
1831
{% include 'includes/form.html' %}
1932
<button type="submit" class="btn btn-success">Post a reply</button>
@@ -30,7 +43,7 @@
3043
<small class="text-muted">{{ post.createdAt }}</small>
3144
</div>
3245
</div>
33-
{{ post.message }}
46+
{{ post.get_message_as_markdown }}
3447
</div>
3548
</div>
3649
{% endfor %}

‎templates/topic_posts.html

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44

55
{% block title %} {{ topic.subject }} {% endblock %}
66

7+
8+
{% block stylesheet %}
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
10+
{% endblock %}
11+
12+
{% block javascript %}
13+
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
14+
<script>
15+
var simplemde = new SimpleMDE();
16+
</script>
17+
{% endblock %}
18+
719
{% block breadcrumb %}
820
<li class="breadcrumb-item"><a href="{% url 'home' %}">Web Chat Boards</a></li>
921
<li class="breadcrumb-item"><a href="{% url 'board_topic' topic.boardName.pk %}">{{ topic.boardName.name }}</a></li>
1022
<li class="breadcrumb-item active">{{ topic.subject }}</li>
11-
1223
{% endblock %}
1324

1425
{% block content %}
@@ -17,7 +28,8 @@
1728
<a href="{% url 'reply_topic' topic.boardName.pk topic.pk %}" class="btn btn-primary" role="button">Reply</a>
1829
</div>
1930

20-
{% for post in topic.posts.all %}
31+
32+
{% for post in posts %}
2133
<div class="card mb-2 {% if forloop.first %} border-dark{% endif %}">
2234
{% if forloop.first %}
2335
<div class="card-header text-white bg-dark py-2 px-3">{{ topic.subject}}</div>
@@ -39,7 +51,7 @@
3951
<small class="text-muted">{{ post.createdAt }}</small>
4052
</div>
4153
</div>
42-
{{ post.message }}
54+
{{ post.get_message_as_markdown }}
4355
{% if post.createdBy == user %}
4456
<div class="mt-3">
4557
<a href="{% url 'edit_post' post.topic.boardName.pk post.topic.pk post.pk %}"
@@ -53,6 +65,8 @@
5365
</div>
5466
{% endfor %}
5567

68+
{% include 'includes/pagination.html' %}
69+
5670
{% endblock %}
5771

58-
<!--https://www.iconfinder.com/search/icons?q=avatar&price=free&license=2-->
72+
<!--https://www.iconfinder.com/search/icons?q=avatar&price=free&license=2-->
272 Bytes
Binary file not shown.
781 Bytes
Binary file not shown.

‎webchat/models.py

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from django.db import models
33
from django.contrib.auth.models import User
44
from django.utils.text import Truncator
5+
from markdown import markdown
6+
from django.utils.html import mark_safe
57

68
class ChatBoard(models.Model):
79
name = models.CharField(max_length = 35, unique = True)
@@ -39,3 +41,6 @@ class Post(models.Model):
3941
def __str__(self):
4042
truncated_message = Truncator(self.message)
4143
return truncated_message.chars(35)
44+
45+
def get_message_as_markdown(self):
46+
return mark_safe(markdown(self.message, sefe_mode='escape'))

‎webchat/views.py

+66-20
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,58 @@
1616
from django.core.paginator import Paginator
1717
from django.core.paginator import EmptyPage
1818
from django.core.paginator import PageNotAnInteger
19+
from django.views.generic import ListView
20+
from django.urls import reverse_lazy
1921

2022

21-
23+
#function based view home- FBC
2224
def home(request):
2325
chatBoard = ChatBoard.objects.all()
2426
return render(request, 'home.html',{'chatBoard':chatBoard})
2527

26-
def board_topic(request,pk):
27-
#chat_board = ChatBoard.objects.get(pk=pk)
28-
chat_board = get_object_or_404(ChatBoard, pk=pk)
28+
#class based view -home
29+
# class BoardListView(ListView):
30+
# model = ChatBoard
31+
# context_object_name = 'boards'
32+
# template_name = 'home.html'
33+
34+
35+
#FBV -board_topic
36+
# def board_topic(request,pk):
37+
# #chat_board = ChatBoard.objects.get(pk=pk)
38+
# chat_board = get_object_or_404(ChatBoard, pk=pk)
39+
40+
# queryset = chat_board.topics.order_by('-lastUpdate').annotate(replies=Count('posts') -1)
41+
# page = request.GET.get('page',1)
42+
43+
# paginator = Paginator(queryset, 20)
2944

30-
queryset = chat_board.topics.order_by('-lastUpdate').annotate(replies=Count('posts') -1)
31-
page = request.GET.get('page',1)
45+
# try:
46+
# topics = paginator.page(page)
47+
# except PageNotAnInteger:
48+
# topics = paginator.page(1)
49+
# except EmptyPage:
50+
# topics = paginator.page(paginator.num_pages)
3251

33-
paginator = Paginator(queryset, 10)
52+
# #topics = chat_board.topics.order_by('-lastUpdate').annotate(replies=Count('posts')-1)
53+
# return render(request, 'chat_board_topics.html', {'chat_board': chat_board, 'topics':topics})
3454

35-
try:
36-
topics = paginator.page(page)
37-
except PageNotAnInteger:
38-
topics = paginator.page(1)
39-
except EmptyPage:
40-
topics = paginator.page(paginator.num_pages)
55+
#CBV -board_topic
56+
class TopicListView(ListView):
57+
model = ChatTopic
58+
context_object_name = 'topics'
59+
template_name = 'chat_board_topics.html'
60+
paginate_by = 20
4161

62+
def get_context_data(self, **kwargs):
63+
kwargs['chat_board'] = self.chat_board
64+
return super().get_context_data(**kwargs)
65+
66+
def get_queryset(self):
67+
self.chat_board = get_object_or_404(ChatBoard, pk = self.kwargs.get('pk'))
68+
queryset = self.chat_board.topics.order_by('-lastUpdate').annotate(replies=Count('posts') - 1)
69+
return queryset
4270

43-
#topics = chat_board.topics.order_by('-lastUpdate').annotate(replies=Count('posts')-1)
44-
return render(request, 'chat_board_topics.html', {'chat_board': chat_board, 'topics':topics})
4571

4672
@login_required
4773
def new_board_topic(request, pk):
@@ -67,12 +93,30 @@ def new_board_topic(request, pk):
6793
return render(request, 'new_board_topic.html', {'chat_board':chat_board, 'form':form})
6894

6995

96+
#FBV-topic_posts
97+
# def topic_posts(request, pk, topic_pk):
98+
# topic = get_object_or_404(ChatTopic, boardName__pk=pk, pk=topic_pk )
99+
# topic.views += 1
100+
# topic.save()
101+
# return render(request, 'topic_posts.html', {'topic':topic})
70102

71-
def topic_posts(request, pk, topic_pk):
72-
topic = get_object_or_404(ChatTopic, boardName__pk=pk, pk=topic_pk )
73-
topic.views += 1
74-
topic.save()
75-
return render(request, 'topic_posts.html', {'topic':topic})
103+
#CBV -topic_posts
104+
class PostListView(ListView):
105+
model = Post
106+
context_object_name = 'posts'
107+
template_name = 'topic_posts.html'
108+
paginate_by = 3
109+
110+
def get_context_data(self, **kwargs):
111+
self.topic.views += 1
112+
self.topic.save()
113+
kwargs['topic'] = self.topic
114+
return super().get_context_data(**kwargs)
115+
116+
def get_queryset(self):
117+
self.topic = get_object_or_404(ChatTopic,boardName__pk=self.kwargs.get('pk'), pk=self.kwargs.get('topic_pk'))
118+
queryset = self.topic.posts.order_by('createdAt')
119+
return queryset
76120

77121
@login_required
78122
def reply_topic(request, pk, topic_pk):
@@ -89,6 +133,8 @@ def reply_topic(request, pk, topic_pk):
89133
form = PostForm()
90134
return render(request,'reply_topic.html', {'topic':topic, 'form':form})
91135

136+
137+
#CBV
92138
@method_decorator(login_required, name='dispatch')
93139
class PostUpdateView(UpdateView):
94140
model = Post

0 commit comments

Comments
 (0)
Please sign in to comment.