Skip to content

Commit be06531

Browse files
committed
configure htmx on search and paginators html
1 parent 2e18c9a commit be06531

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

apps/users/forms.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ class RegisterForm(forms.ModelForm):
4040
widget=forms.PasswordInput(attrs={"id": "password", "type": "password"}),
4141
)
4242

43-
def save(self, commit=True):
44-
user = super().save(commit)
45-
43+
def clean_password2(self):
4644
password1 = self.cleaned_data.get("password1")
4745
password2 = self.cleaned_data.get("password2")
46+
if password1 and password2 and password1 != password2:
47+
raise ValidationError("Passwords must be match!")
48+
return password2
49+
50+
def save(self, commit=True):
51+
user = super().save(commit=False)
4852

49-
if password1 == password2:
50-
user.set_password(password1)
51-
user.save()
52-
else:
53-
return ValidationError("Passwords must be match!")
53+
user.set_password(self.cleaned_data["password1"])
54+
user.save()
55+
return user
5456

5557
def __init__(self, *args, **kwargs):
5658
super().__init__(*args, **kwargs)
@@ -59,9 +61,4 @@ def __init__(self, *args, **kwargs):
5961

6062
class Meta:
6163
model = User
62-
fields = (
63-
"username",
64-
"password1",
65-
"password2",
66-
"email",
67-
)
64+
fields = ("username", "password1", "password2", "email", )

apps/users/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class RegisterPageView(CustomHtmxMixin, View):
1919
def get(self, request):
2020
context = {
2121
"title": "Registration",
22-
"tempate_htmx": self.template_htmx,
22+
"template_htmx": self.template_htmx,
2323
"form": RegisterForm()
2424
}
2525
return render(request, self.template_name, context)

templates/auth/register.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ <h2>Register</h2>
66

77
<form
88
action="{% url 'users:register' %}"
9+
hx-post="{% url 'users:register' %}"
10+
hx-push-url="true"
11+
hx-target="#main-content"
912
method="post"
1013
enctype="multipart/form-data">
1114
{% csrf_token %}

templates/components/headers.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,24 @@
128128
</ul>
129129

130130
<!-- Search posts -->
131-
<form action="{% url 'blog:home' %}" >
131+
<form
132+
action="{% url 'blog:home' %}"
133+
hx-get="{% url 'blog:home' %}"
134+
hx-push-url="true"
135+
hx-target="#main-content">
132136
<input
133-
class="form-control"
134-
type="text"
137+
class="form-control me-2"
138+
type="search"
135139
name="search_query"
136140
id="search-posts"
137141
placeholder="Search"
138142
value="{% if search_query_value %}{{ search_query_value }}{% endif %}"
139143
aria-label="Search"
144+
hx-get="{% url 'blog:home' %}"
145+
hx-trigger="keyup changed delay:150ms"
146+
hx-triget="#main-content"
140147
>
141148
</form>
142-
<!-- hx-get="{% url 'blog:home' %}"
143-
hx-trigger="keyup changed delay:150ms"
144-
hx-triget="#main-content" -->
145149

146150
</div>
147151
</div>

templates/components/paginators.html

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
<!-- Кнопка "Назад" -->
66
{% if page_obj.has_previous %}
77
<li class="page-item">
8-
<a class="page-link" href="{% url 'blog:home' %}?page={{ page_obj.previous_page_number }}&size={{ size_value }}" aria-label="Previous">
8+
<a
9+
class="page-link"
10+
href="{% url 'blog:home' %}?page={{ page_obj.previous_page_number }}&size={{ size_value }}"
11+
hx-get="{% url 'blog:home' %}?page={{ page_obj.previous_page_number }}&size={{ size_value }}"
12+
hx-push-url="true"
13+
hx-target="#main-content"
14+
aria-label="Previous">
915
<span aria-hidden="true">&laquo;</span>
1016
</a>
1117
</li>
@@ -23,11 +29,23 @@
2329
</li>
2430
{% elif i >= page_obj.number|add:'-2' and i <= page_obj.number|add:'2' %}
2531
<li class="page-item">
26-
<a class="page-link" href="{% url 'blog:home' %}?page={{ i }}&size={{ size_value }}">{{ i }}</a>
32+
<a
33+
class="page-link"
34+
href="{% url 'blog:home' %}?page={{ i }}&size={{ size_value }}"
35+
hx-get="{% url 'blog:home' %}?page={{ i }}&size={{ size_value }}"
36+
hx-push-url="true"
37+
hx-target="#main-content"
38+
>{{ i }}</a>
2739
</li>
2840
{% elif i == 1 or i == page_obj.paginator.num_pages %}
2941
<li class="page-item">
30-
<a class="page-link" href="{% url 'blog:home' %}?page={{ i }}&size={{ size_value }}">{{ i }}</a>
42+
<a
43+
class="page-link"
44+
href="{% url 'blog:home' %}?page={{ i }}&size={{ size_value }}"
45+
hx-get="{% url 'blog:home' %}?page={{ i }}&size={{ size_value }}"
46+
hx-push-url="true"
47+
hx-target="#main-content"
48+
>{{ i }}</a>
3149
</li>
3250
{% elif i == page_obj.number|add:'-3' or i == page_obj.number|add:'3' %}
3351
<li class="page-item disabled"><span class="page-link">...</span></li>
@@ -37,7 +55,12 @@
3755
<!-- Кнопка "Вперед" -->
3856
{% if page_obj.has_next %}
3957
<li class="page-item">
40-
<a class="page-link" href="{% url 'blog:home' %}?page={{ page_obj.next_page_number }}&size={{ size_value }}" aria-label="Next">
58+
<a class="page-link"
59+
href="{% url 'blog:home' %}?page={{ page_obj.next_page_number }}&size={{ size_value }}"
60+
hx-get="{% url 'blog:home' %}?page={{ page_obj.next_page_number }}&size={{ size_value }}"
61+
hx-push-url="true"
62+
hx-target="#main-content"
63+
aria-label="Next">
4164
<span aria-hidden="true">&raquo;</span>
4265
</a>
4366
</li>

0 commit comments

Comments
 (0)