Commit 7ffdabc2 authored by Давид Ли's avatar Давид Ли

lesson 52

parent 8dcdb18e
......@@ -21,7 +21,7 @@ from webapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.IndexRedirectView.as_view(), name='redirect_to_index'),
path('articles', views.IndexView.as_view(), name='index'),
path('articles', views.IndexListView.as_view(), name='index'),
path('articles/add', views.ArticleCreateView.as_view(), name='articles_add'),
path('articles/<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('articles/<int:id>/update', views.ArticleUpdateView.as_view(), name='articles_update'),
......
......@@ -34,6 +34,10 @@
<a class="nav-link" href="#">Disabled</a>
</li>
</ul>
<form class="d-flex">
<input class="form-control me-2" name="search" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
......
......@@ -12,6 +12,9 @@
<hr>
{% endfor %}
</div>
{% if is_paginated %}
{% include 'partial/pagination.html' %}
{% endif %}
{% endblock %}
\ No newline at end of file
<div class="d-flex justify-content-center">
<nav aria-label="...">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?{% if query_params %}{{ query_params }}{% endif %}&page={{ page_obj.previous_page_number }}">
Previous
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#">Previous</a>
</li>
{% endif %}
{% for num in paginator.page_range %}
{% if page_obj.number == num %}
<li class="page-item active" aria-current="page">
<a class="page-link" href="?{% if query_params %}{{ query_params }}{% endif %}&page={{ num }}">{{ num }}</a>
</li>
{% else %}
<li class="page-item" aria-current="page">
<a class="page-link" href="?{% if query_params %}{{ query_params }}{% endif %}&page={{ num }}">{{ num }}</a>
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?{% if query_params %}{{ query_params }}{% endif %}&page={{ page_obj.next_page_number }}">
Next
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#">Next</a>
</li>
{% endif %}
</ul>
</nav>
</div>
\ No newline at end of file
<form class="text-center" action="" method="get">
<label for="{{ form.search.id_for_label }}">
{{ form.search.label }}:
</label>
{{ form.search }}
<button class="btn btn-submit">Search</button>
</form>
\ No newline at end of file
......@@ -14,3 +14,7 @@ class ArticleForm(forms.ModelForm):
raise ValidationError('Text should not duplicate title')
return super().clean()
class SearchForm(forms.Form):
search = forms.CharField(max_length=100, required=False, label='Найти')
from typing import Any
from urllib.parse import urlencode
from django.db.models.query import QuerySet
from django.http.response import HttpResponse as HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from django.views import generic
from django.db.models import Q
from webapp.forms import ArticleForm
from webapp.forms import ArticleForm, SearchForm
from webapp.models import Article
......@@ -19,6 +22,45 @@ class IndexView(generic.View):
return render(request, 'index.html', context)
class IndexListView(generic.ListView):
template_name = 'index.html'
context_object_name = 'articles'
model = Article
ordering = ['-created_at']
paginate_by = 5
paginate_orphans = 1
def get(self, request, *args, **kwargs):
self.form = SearchForm(self.request.GET)
self.search_value = None
if self.form.is_valid():
self.search_value = self.form.cleaned_data['search']
return super().get(request, *args, **kwargs)
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(object_list=None, **kwargs)
context['form'] = self.form
if self.search_value:
context['query_params'] = urlencode({'search': self.search_value})
return context
def get_queryset(self):
qs = super().get_queryset()
if self.search_value:
query = (
Q(title__icontains=self.search_value)
| Q(author__icontains=self.search_value)
)
qs = qs.filter(query).distinct()
return qs
class ArticleDetailView(generic.TemplateView):
template_name = 'articles/detail.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