Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-12_django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
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
Давид Ли
ap-12_django
Commits
7ffdabc2
Commit
7ffdabc2
authored
Feb 05, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 52
parent
8dcdb18e
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
108 additions
and
3 deletions
+108
-3
urls.py
ap_12/core/urls.py
+1
-1
base.html
ap_12/templates/base.html
+4
-0
index.html
ap_12/templates/index.html
+4
-1
pagination.html
ap_12/templates/partial/pagination.html
+44
-0
search.html
ap_12/templates/partial/search.html
+8
-0
forms.py
ap_12/webapp/forms.py
+4
-0
views.py
ap_12/webapp/views.py
+43
-1
No files found.
ap_12/core/urls.py
View file @
7ffdabc2
...
...
@@ -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
.
Index
List
View
.
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'
),
...
...
ap_12/templates/base.html
View file @
7ffdabc2
...
...
@@ -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>
...
...
ap_12/templates/index.html
View file @
7ffdabc2
...
...
@@ -13,5 +13,8 @@
{% endfor %}
</div>
{% if is_paginated %}
{% include 'partial/pagination.html' %}
{% endif %}
{% endblock %}
\ No newline at end of file
ap_12/templates/partial/pagination.html
0 → 100644
View file @
7ffdabc2
<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
ap_12/templates/partial/search.html
0 → 100644
View file @
7ffdabc2
<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
ap_12/webapp/forms.py
View file @
7ffdabc2
...
...
@@ -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
=
'Найти'
)
ap_12/webapp/views.py
View file @
7ffdabc2
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'
...
...
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