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
021be428
Commit
021be428
authored
Feb 08, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 53
parent
7ffdabc2
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
201 additions
and
3 deletions
+201
-3
context_processors.py
ap_12/core/context_processors.py
+5
-0
settings.py
ap_12/core/settings.py
+1
-0
urls.py
ap_12/core/urls.py
+1
-0
detail.html
ap_12/templates/articles/detail.html
+32
-0
index.html
ap_12/templates/index.html
+3
-1
form.html
ap_12/templates/partial/form.html
+1
-1
admin.py
ap_12/webapp/admin.py
+2
-1
forms.py
ap_12/webapp/forms.py
+6
-0
__init__.py
ap_12/webapp/views/__init__.py
+59
-0
articles.py
ap_12/webapp/views/articles.py
+68
-0
comments.py
ap_12/webapp/views/comments.py
+23
-0
No files found.
ap_12/core/context_processors.py
0 → 100644
View file @
021be428
from
webapp
import
models
def
articles
(
request
):
return
{
'articles_'
:
models
.
Article
.
objects
.
all
()[:
10
]}
ap_12/core/settings.py
View file @
021be428
...
...
@@ -64,6 +64,7 @@ TEMPLATES = [
'django.template.context_processors.request'
,
'django.contrib.auth.context_processors.auth'
,
'django.contrib.messages.context_processors.messages'
,
'core.context_processors.articles'
,
],
},
},
...
...
ap_12/core/urls.py
View file @
021be428
...
...
@@ -26,4 +26,5 @@ urlpatterns = [
path
(
'articles/<int:id>'
,
views
.
ArticleDetailView
.
as_view
(),
name
=
'articles_detail'
),
path
(
'articles/<int:id>/update'
,
views
.
ArticleUpdateView
.
as_view
(),
name
=
'articles_update'
),
path
(
'articles/<int:id>/delete'
,
views
.
article_delete_view
,
name
=
'articles_delete'
),
path
(
'articles/<int:article_id>/comments/add'
,
views
.
CommentCreateView
.
as_view
(),
name
=
'comments_add'
),
]
ap_12/templates/articles/detail.html
View file @
021be428
...
...
@@ -27,4 +27,36 @@
<button
class=
"btn btn-danger"
>
Delete
</button>
</form>
<hr>
<h3
class=
"mb-4"
>
Comments
</h3>
{% url 'comments_add' article_id=article.id as comments_action_url %}
{% include 'partial/form.html' with form=comments_form request_method='post' action_url=comments_action_url %}
{% for comment in comments %}
<div
class=
"card my-4"
>
<div
class=
"card-header"
>
{{ comment.author }}
</div>
<div
class=
"card-body"
>
<blockquote
class=
"blockquote mb-0"
>
<p>
{{ comment.text }}
</p>
<footer
class=
"blockquote-footer"
>
<cite
title=
"Source Title"
>
{{ article.created_at }}
</cite>
</footer>
</blockquote>
</div>
</div>
{% empty %}
<h5>
No comments yet.
</h5>
{% endfor %}
{% endblock %}
\ No newline at end of file
ap_12/templates/index.html
View file @
021be428
...
...
@@ -10,6 +10,8 @@
<h2>
{{ article.title }}
</h2>
<a
href=
"{% url 'articles_detail' id=article.id %}"
>
Подробнее...
</a>
<hr>
{% empty %}
<h2>
no articles yet ...
</h2>
{% endfor %}
</div>
...
...
ap_12/templates/partial/form.html
View file @
021be428
{% if not fields_only %}
<form
method=
"{{ request_method|default:'get' }}"
>
<form
method=
"{{ request_method|default:'get' }}"
action=
"{{ action_url|default:'' }}"
>
{% csrf_token %}
{% endif %}
...
...
ap_12/webapp/admin.py
View file @
021be428
...
...
@@ -2,7 +2,8 @@ from django.contrib import admin
from
webapp
import
models
# admin.site.register(models.Article, ArticleAdmin)
admin
.
site
.
register
(
models
.
Comment
)
@
admin
.
register
(
models
.
Article
)
class
ArticleAdmin
(
admin
.
ModelAdmin
):
...
...
ap_12/webapp/forms.py
View file @
021be428
...
...
@@ -18,3 +18,9 @@ class ArticleForm(forms.ModelForm):
class
SearchForm
(
forms
.
Form
):
search
=
forms
.
CharField
(
max_length
=
100
,
required
=
False
,
label
=
'Найти'
)
class
CommentForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
models
.
Comment
fields
=
(
'text'
,
'author'
)
ap_12/webapp/views/__init__.py
0 → 100644
View file @
021be428
from
urllib.parse
import
urlencode
from
django.http.response
import
HttpResponse
as
HttpResponse
from
django.views
import
generic
from
django.db.models
import
Q
from
webapp.forms
import
SearchForm
from
webapp.models
import
Article
from
webapp.views.articles
import
(
ArticleCreateView
,
ArticleDetailView
,
ArticleUpdateView
,
article_delete_view
,
)
from
webapp.views.comments
import
(
CommentCreateView
,
)
class
IndexRedirectView
(
generic
.
RedirectView
):
pattern_name
=
'index'
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
ap_12/webapp/views.py
→
ap_12/webapp/views
/articles
.py
View file @
021be428
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.shortcuts
import
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
,
Search
Form
from
webapp.forms
import
ArticleForm
,
Comment
Form
from
webapp.models
import
Article
class
IndexRedirectView
(
generic
.
RedirectView
):
pattern_name
=
'index'
class
IndexView
(
generic
.
View
):
def
get
(
self
,
request
):
context
=
{
'articles'
:
Article
.
objects
.
all
()}
return
render
(
request
,
'index.html'
,
context
)
class
IndexListView
(
generic
.
ListView
):
template_name
=
'index.html'
context_object_name
=
'articles'
class
ArticleDetailView
(
generic
.
DetailView
):
pk_url_kwarg
=
'id'
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'
def
get_context_data
(
self
,
**
kwargs
):
kwargs
[
'article'
]
=
get_object_or_404
(
Article
,
id
=
kwargs
[
'id'
])
return
super
()
.
get_context_data
(
**
kwargs
)
comments
=
self
.
object
.
comments
.
order_by
(
'-created_at'
)
return
super
()
.
get_context_data
(
**
kwargs
,
comments
=
comments
,
comments_form
=
CommentForm
,
)
class
ArticleCreateView
(
generic
.
FormView
):
class
ArticleCreateView
(
generic
.
CreateView
):
model
=
Article
template_name
=
'articles/article.html'
form_class
=
ArticleForm
def
form_valid
(
self
,
form
):
self
.
article
=
form
.
save
()
return
super
()
.
form_valid
(
form
)
def
get_success_url
(
self
):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
article
.
id
})
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
object
.
id
})
class
ArticleUpdateView
(
generic
.
FormView
):
...
...
@@ -112,41 +62,6 @@ class ArticleUpdateView(generic.FormView):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
article
.
id
})
def
article_update_view
(
request
,
id
):
article
=
get_object_or_404
(
Article
,
id
=
id
)
if
request
.
method
==
'GET'
:
form
=
ArticleForm
(
initial
=
{
'title'
:
article
.
title
,
'text'
:
article
.
text
,
'author'
:
article
.
author
,
'tags'
:
article
.
tags
.
all
(),
})
return
render
(
request
,
'articles/article.html'
,
context
=
{
'article'
:
article
,
'form'
:
form
},
)
elif
request
.
method
==
'POST'
:
form
=
ArticleForm
(
data
=
request
.
POST
)
if
form
.
is_valid
():
tags
=
form
.
cleaned_data
.
pop
(
'tags'
)
article
.
title
=
form
.
cleaned_data
.
get
(
'title'
)
article
.
author
=
form
.
cleaned_data
.
get
(
'author'
)
article
.
text
=
form
.
cleaned_data
.
get
(
'text'
)
article
.
save
()
article
.
tags
.
set
(
tags
)
return
redirect
(
'index'
)
else
:
return
render
(
request
,
'articles/article.html'
,
context
=
{
'form'
:
form
,
'article'
:
article
})
def
article_delete_view
(
request
,
id
):
get_object_or_404
(
Article
,
id
=
id
)
.
delete
()
...
...
ap_12/webapp/views/comments.py
0 → 100644
View file @
021be428
from
django.shortcuts
import
redirect
from
django.forms.models
import
BaseModelForm
from
django.http
import
HttpResponse
from
django.views
import
generic
from
webapp.models
import
Comment
,
Article
from
webapp.forms
import
CommentForm
class
CommentCreateView
(
generic
.
CreateView
):
model
=
Comment
template_name
=
'comments/comment.html'
form_class
=
CommentForm
def
form_valid
(
self
,
form
):
# article = get_object_or_404(Article, id=self.kwargs.get(''))
article_id
=
self
.
kwargs
.
get
(
'article_id'
)
comment
=
form
.
save
(
commit
=
False
)
comment
.
article_id
=
article_id
comment
.
save
()
return
redirect
(
'articles_detail'
,
id
=
article_id
)
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