Skip to content

Commit 37139a3

Browse files
Merge pull request #16 from RustamovAkrom/main
Main
2 parents 565b003 + 79101ea commit 37139a3

File tree

10 files changed

+123
-55
lines changed

10 files changed

+123
-55
lines changed

apps/blog/utils.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,11 @@ def get_search_model_queryset(
1616
)
1717

1818
return search_query
19-
# search_for_title = model_queryset.filter(title__icontains=search_query)
20-
# if not search_for_title:
21-
# search_for_content = model_queryset.filter(content__icontains=search_query)
22-
# if not search_for_content:
23-
# queryset = search_for_content
24-
# else:
25-
# queryset = search_for_content
26-
# else:
27-
# queryset = search_for_title
28-
# return queryset
19+
2920

3021

3122
def get_pagination_obj(model_queryset: QuerySet, page: int = 1, size: int = 4) -> Page:
32-
paginator = Paginator(model_queryset.order_by("id"), size)
23+
paginator = Paginator(model_queryset.order_by("-created_at"), size)
3324

3425
try:
3526
page_obj = paginator.page(page)

apps/blog/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def get(self, request):
166166
context = {
167167
"title": "My posts",
168168
"template_htmx": self.template_htmx,
169-
"posts": posts
169+
"posts": posts.order_by("-created_at")
170170
}
171171
return render(request, self.template_name, context)
172172

templates/base.html

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,7 @@
2121
{% include 'components/headers.html' %}
2222
<div class="mt-5">
2323
<!-- Messages -->
24-
25-
{% if messages %}
26-
{% for message in messages %}
27-
{% if message.tags == 'error' %}
28-
<div class="alert alert-danger alert-dismissible fade show" role="alert">
29-
{{ message }}
30-
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
31-
</div>
32-
{% else %}
33-
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
34-
{{ message }}
35-
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
36-
</div>
37-
{% endif %}
38-
{% endfor %}
39-
{% endif %}
24+
{% include 'components/messages.html' %}
4025

4126
<!-- Content -->
4227
<div id="main-content">
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
{% extends 'base.html' %}
2-
{% load static %}
31

4-
{% block title %} Post Confirm Delete {% endblock %}
5-
6-
{% block content %}
72
<main role="main" class="container">
83
<div class="row">
94
<div class="col-md-8">
@@ -16,14 +11,19 @@ <h4>"{{ post.title }}" ?</h4>
1611
</fieldset>
1712
<div class="form-group">
1813
<button class="btn btn-outline-danger" type="submit">Yes, Delete</button>
19-
<a class="btn btn-outline-secondary" href="{{ post.get_absolute_url }}#content">Cancel</a>
14+
<a
15+
class="btn btn-outline-secondary"
16+
href="{{ post.get_absolute_url }}#content"
17+
hx-get="{{ post.get_absolute_url }}#content"
18+
hx-push-url="true"
19+
hx-target="#main-content"
20+
>Cancel</a>
2021
</div>
2122
</form>
2223
</div>
2324
</div>
2425
{% include 'components/latest_posts.html' %}
2526
</div>
2627
</main>
27-
{% endblock %}
2828

2929

templates/blog/post_detail.html

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
<div class="col-md-8">
66
<article class="blog-post">
77
<h2 class="blog-post-title">{{ post.title }}</h2>
8-
<p class="blog-post-meta">{{ post.publisher_at }}by <a href="{% url 'users:user_profile' post.author.username %}">{{ post.author }}</a></p>
8+
<p class="blog-post-meta">{{ post.publisher_at }}by
9+
<a
10+
href="{% url 'users:user_profile' post.author.username %}"
11+
hx-get="{% url 'users:user_profile' post.author.username %}"
12+
hx-push-url="true"
13+
hx-target="#main-content"
14+
>{{ post.author }}</a>
15+
</p>
916

1017
<div class="text">
1118
<p><small>{{ post.description }}</small></p>
@@ -19,19 +26,34 @@ <h2 class="blog-post-title">{{ post.title }}</h2>
1926

2027
<!-- Watching count-->
2128
<div class="mb-2">
22-
<a href="#main-content" class="nav-link text-dark">
29+
<a
30+
href="#main-content"
31+
hx-get="#main-content"
32+
hx-push-url="true"
33+
hx-target="#main-content"
34+
class="nav-link text-dark">
2335
<i class="bi bi-eye">({{ post.watching }})</i>
2436
</a>
2537
</div>
2638
<!-- Comments count -->
2739
<div class="mb-2">
28-
<a href="#post_comments" class="nav-link text-dark">
40+
<a
41+
href="#post_comments"
42+
hx-get="#post_comments"
43+
hx-push-url="true"
44+
hx-target="#main-content"
45+
class="nav-link text-dark">
2946
<i class="bi bi-chat-left-text">({{ post.comment_count }})</i>
3047
</a>
3148
</div>
3249
<!-- Like count -->
3350
<div class="md-2" id="post_like_count">
34-
<a href="{% url 'blog:post_like' post.slug %}#post_like_count" class="nav-link text-dark">
51+
<a
52+
href="{% url 'blog:post_like' post.slug %}#post_like_count"
53+
hx-get="{% url 'blog:post_like' post.slug %}#post_like_count"
54+
hx-push-url="true"
55+
hx-target="#main-content"
56+
class="nav-link text-dark">
3557
{% if request.user.is_authenticated %}
3658
{% if post|check_like:request.user %}
3759
<i class="bi bi-star-fill">({{ post.like_count }})</i>
@@ -45,7 +67,12 @@ <h2 class="blog-post-title">{{ post.title }}</h2>
4567
</div>
4668
<!-- Dislike count -->
4769
<div class="md-2" id="post_dislike_count">
48-
<a href="{% url 'blog:post_dislike' post.slug %}#post_dislike_count" class="nav-link text-dark">
70+
<a
71+
href="{% url 'blog:post_dislike' post.slug %}#post_dislike_count"
72+
hx-get="{% url 'blog:post_dislike' post.slug %}#post_dislike_count"
73+
hx-push-url="true"
74+
hx-target="#main-content"
75+
class="nav-link text-dark">
4976
{% if request.user.is_authenticated %}
5077
{% if post|check_dislike:request.user %}
5178
<i class="bi bi-emoji-frown-fill">({{ post.dislike_count }})</i>
@@ -61,13 +88,30 @@ <h2 class="blog-post-title">{{ post.title }}</h2>
6188
<!-- Delete, Update buttons -->
6289
{% if request.user == post.author %}
6390
<nav class="blog-pagination mt-3 mb-3" aria-label="Pagination">
64-
<a class="btn btn-outline-danger" href="{% url 'blog:post_delete' post.slug %}">delete</a>
65-
<a class="btn btn-outline-warning" href="{% url 'blog:post_update' post.slug %}">update</a>
91+
<a
92+
class="btn btn-outline-danger"
93+
href="{% url 'blog:post_delete' post.slug %}"
94+
hx-get="{% url 'blog:post_delete' post.slug %}"
95+
hx-push-url="true"
96+
hx-target="#main-content"
97+
>delete</a>
98+
<a
99+
class="btn btn-outline-warning"
100+
href="{% url 'blog:post_update' post.slug %}"
101+
hx-get="{% url 'blog:post_update' post.slug %}"
102+
hx-push-url="true"
103+
hx-target="#main-content"
104+
>update</a>
66105
</nav>
67106
{% endif %}
68107
<!-- Send comment message -->
69108
<div id="post_comments_form">
70-
<form action="{% url 'blog:post_message' post.slug %}#post_comments_form" class="mt-3 mb-3">
109+
<form
110+
action="{% url 'blog:post_message' post.slug %}#post_comments_form"
111+
hx-get="{% url 'blog:post_message' post.slug %}#post_comments_form"
112+
hx-push-url="true"
113+
hx-target="#main-content"
114+
class="mt-3 mb-3">
71115
{% csrf_token %}
72116
<div class="d-flex">
73117
<input type="text" name="post_message_input" class="form-control" placeholder="message..." required="">

templates/blog/post_update.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<main role="main" class="container">
22
<div class="row">
33
<div class="col-md-8">
4-
<form action="{% url 'blog:post_update' post.slug %}" method="post">
4+
<form
5+
action="{% url 'blog:post_update' post.slug %}"
6+
method="post">
57
{% csrf_token %}
68
<fieldset class="form-group">
79
<legend class="border-bottom mb-4"><b>Update post</b>: {{ post.title}}</legend>
@@ -26,7 +28,12 @@
2628
</div>
2729
</fieldset>
2830
<div class="mt-3">
29-
<a href="{{ post.get_absolute_url }}#content" class="btn btn-outline-secondary">Cancel</a>
31+
<a
32+
href="{{ post.get_absolute_url }}#content"
33+
hx-get="{{ post.get_absolute_url }}#content"
34+
hx-push-url="true"
35+
hx-target="#main-content"
36+
class="btn btn-outline-secondary">Cancel</a>
3037
<button type="submit" class="btn btn-outline-primary">Update</button>
3138
</div>
3239
</form>

templates/blog/profile.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ <h3>
3333

3434
{% if request.user == user %}
3535
<div class="col">
36-
<a href="{% url 'blog:post_create' %}" class="btn btn-outline-secondary">Create New Post</a>
36+
<a
37+
href="{% url 'blog:post_create' %}"
38+
hx-get="{% url 'blog:post_create' %}"
39+
hx-push-url="true"
40+
hx-target="#main-content"
41+
class="btn btn-outline-secondary">Create New Post</a>
3742
</div>
3843
{% endif %}
3944
<div class="col">
@@ -56,7 +61,13 @@ <h3>
5661
<h3 class="mb-0">{{ post.title }}</h3>
5762
<div class="mb-1 text-muted">{{ post.publisher_at }}</div>
5863
<p class="card-text mb-auto">{{ post.description|truncatewords_html:30 }}</p>
59-
<a href="{{ post.get_absolute_url }}" class="stretched-link">Continue reading</a>
64+
<a
65+
href="{{ post.get_absolute_url }}"
66+
hx-get="{{ post.get_absolute_url }}"
67+
hx-push-url="true"
68+
hx-target="#main-content"
69+
class="stretched-link"
70+
>Continue reading</a>
6071
<div class="d-flex">
6172
<strong class="d-inline-block mb-2 col text-primary"><i class="bi bi-eye">{{ post.watching }}</i></strong>
6273
<strong class="d-inline-block mb-2 col text-primary"><i class="bi bi-star">{{ post.like_count }}</i></strong>

templates/blog/user_posts.html

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<main role="main" class="container">
22
<div class="row">
33
<div class="col-md-8">
4-
<!-- {% for message in massages %}
5-
<div class="alert alert-{{ message.tags }}">
6-
<h6>{{ message }}</h6>
7-
</div>
8-
{% endfor %} -->
94
<div class="d-flex">
105

116
<div class="col">
12-
<a href="{% url 'blog:post_create' %}" class="btn btn-outline-secondary">Create New Post</a>
7+
<a
8+
href="{% url 'blog:post_create' %}"
9+
hx-get="{% url 'blog:post_create' %}"
10+
hx-push-url="true"
11+
hx-target="#main-content"
12+
class="btn btn-outline-secondary">Create New Post</a>
1313
</div>
1414
<div class="col">
15-
<form action="{% url 'blog:user_posts' %}">
15+
<form
16+
action="{% url 'blog:user_posts' %}"
17+
hx-get="{% url 'blog:user_posts' %}"
18+
hx-push-url="true"
19+
hx-target="#main-content">
1620
<div class="d-flex">
1721
<input class="form-control" type="text" name="search_query_for_user_posts" placeholder="Search"
1822
aria-label="Search">
@@ -34,7 +38,12 @@ <h6>{{ message }}</h6>
3438
<h3 class="mb-0">{{ post.title }}</h3>
3539
<div class="mb-1 text-muted">{{ post.publisher_at }}</div>
3640
<p class="card-text mb-auto">{{ post.description|truncatewords_html:30 }}</p>
37-
<a href="{{ post.get_absolute_url }}" class="stretched-link">Continue reading</a>
41+
<a
42+
href="{{ post.get_absolute_url }}"
43+
hx-get="{{ post.get_absolute_url }}"
44+
hx-push-url="true"
45+
hx-target="#main-content"
46+
class="stretched-link">Continue reading</a>
3847
</div>
3948
</div>
4049
</div>

templates/components/headers.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@
146146
hx-triget="#main-content"
147147
>
148148
</form>
149+
<div class="d-flex md-2 ms-2">
150+
151+
<i class="bi bi-search"></i>
152+
</div>
149153

150154
</div>
151155
</div>

templates/components/messages.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% if messages %}
2+
<div id="messages">
3+
{% for message in messages %}
4+
{% if message.tags == 'error' %}
5+
<div class="alert alert-danger alert-dismissible fade show" role="alert">
6+
{{ message }}
7+
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
8+
</div>
9+
{% else %}
10+
<div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
11+
{{ message }}
12+
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
13+
</div>
14+
{% endif %}
15+
{% endfor %}
16+
</div>
17+
{% endif %}

0 commit comments

Comments
 (0)