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
3e240412
Commit
3e240412
authored
Feb 12, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 55
parent
021be428
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
150 additions
and
30 deletions
+150
-30
urls.py
ap_12/core/urls.py
+11
-1
detail.html
ap_12/templates/articles/detail.html
+14
-2
comment.html
ap_12/templates/comments/comment.html
+7
-0
__init__.py
ap_12/webapp/views/__init__.py
+3
-1
articles.py
ap_12/webapp/views/articles.py
+93
-23
comments.py
ap_12/webapp/views/comments.py
+22
-3
No files found.
ap_12/core/urls.py
View file @
3e240412
...
...
@@ -25,6 +25,16 @@ urlpatterns = [
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'
),
path
(
'articles/<int:id>/delete'
,
views
.
article_delete_view
,
name
=
'articles_delete'
),
path
(
'articles/<int:id>/delete'
,
views
.
ArticleDeleteView
.
as_view
()
,
name
=
'articles_delete'
),
path
(
'articles/<int:article_id>/comments/add'
,
views
.
CommentCreateView
.
as_view
(),
name
=
'comments_add'
),
path
(
'articles/<int:article_id>/comments/<int:comment_id>/update'
,
views
.
CommentUpdateView
.
as_view
(),
name
=
'comments_update'
,
),
path
(
'articles/<int:article_id>/comments/<int:comment_id>/delete'
,
views
.
CommentDeleteView
.
as_view
(),
name
=
'comments_delete'
,
),
]
ap_12/templates/articles/detail.html
View file @
3e240412
...
...
@@ -37,8 +37,20 @@
{% for comment in comments %}
<div
class=
"card my-4"
>
<div
class=
"card-header"
>
{{ comment.author }}
<div
class=
"card-header d-flex justify-content-between align-items-center"
>
<div
class=
""
>
{{ comment.author }}
</div>
<div
class=
"d-flex"
>
<a
href=
"{% url 'comments_update' article_id=article.id comment_id=comment.id %}"
class=
"btn btn-warning me-4"
>
Update
</a>
<form
action=
"{% url 'comments_delete' article_id=article.id comment_id=comment.id %}"
method=
"post"
onsubmit=
"return confirm('Delete?')"
>
{% csrf_token %}
<button
class=
"btn btn-danger"
>
Delete
</button>
</form>
</div>
</div>
<div
class=
"card-body"
>
...
...
ap_12/templates/comments/comment.html
0 → 100644
View file @
3e240412
{% extends 'base.html' %}
{% block content %}
{% include 'partial/form.html' with request_method='post' %}
{% endblock %}
ap_12/webapp/views/__init__.py
View file @
3e240412
...
...
@@ -9,10 +9,12 @@ from webapp.views.articles import (
ArticleCreateView
,
ArticleDetailView
,
ArticleUpdateView
,
article_delete_v
iew
,
ArticleDeleteV
iew
,
)
from
webapp.views.comments
import
(
CommentCreateView
,
CommentUpdateView
,
CommentDeleteView
,
)
...
...
ap_12/webapp/views/articles.py
View file @
3e240412
from
typing
import
Any
from
django.http
import
HttpRequest
from
django.http.response
import
HttpResponse
as
HttpResponse
from
django.shortcuts
import
get_object_or_404
,
redirect
from
django.urls
import
reverse
from
django.shortcuts
import
get_object_or_404
,
redirect
,
render
from
django.urls
import
reverse
,
reverse_lazy
from
django.views
import
generic
from
webapp.forms
import
ArticleForm
,
CommentForm
...
...
@@ -31,38 +32,107 @@ class ArticleCreateView(generic.CreateView):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
object
.
id
})
class
ArticleUpdateView
(
generic
.
FormView
):
template_name
=
'articles/article.html'
form_class
=
ArticleForm
class
UpdateView
(
generic
.
View
):
template_name
=
None
model
=
None
form_class
=
None
context_object_name
=
None
pk_url_kwarg
=
None
success_url
=
None
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
article
=
self
.
get_object
()
self
.
object
=
self
.
get_object
()
return
super
()
.
dispatch
(
request
,
*
args
,
**
kwargs
)
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
self
.
form_class
(
instance
=
self
.
object
)
return
self
.
render_to_response
(
form
=
form
)
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
self
.
form_class
(
instance
=
self
.
object
,
data
=
request
.
POST
)
if
form
.
is_valid
():
return
self
.
form_valid
(
form
)
return
self
.
form_invalid
(
form
)
def
get_object
(
self
):
return
get_object_or_404
(
Article
,
id
=
self
.
kwargs
.
get
(
'id'
))
return
get_object_or_404
(
self
.
model
,
id
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
))
def
get_context_data
(
self
,
**
kwargs
):
return
super
()
.
get_context_data
(
**
kwargs
,
article
=
self
.
article
,
btn_text
=
'Update'
,
)
def
get_form_kwargs
(
self
):
kwargs
=
super
()
.
get_form_kwargs
()
kwargs
[
'instance'
]
=
self
.
article
kwargs
[
self
.
context_object_name
]
=
self
.
object
return
kwargs
def
get_success_url
(
self
):
return
self
.
success_url
def
form_valid
(
self
,
form
):
self
.
article
=
form
.
save
()
return
super
()
.
form_valid
(
form
)
self
.
object
=
form
.
save
()
return
redirect
(
self
.
get_success_url
())
def
form_invalid
(
self
,
form
):
return
self
.
render_to_response
(
form
=
form
)
def
render_to_response
(
self
,
**
kwargs
):
return
render
(
self
.
request
,
self
.
template_name
,
context
=
self
.
get_context_data
(
**
kwargs
),
)
class
ArticleUpdateView
(
generic
.
UpdateView
):
template_name
=
'articles/article.html'
model
=
Article
form_class
=
ArticleForm
context_object_name
=
'article'
pk_url_kwarg
=
'id'
extra_context
=
{
'btn_text'
:
'Update'
}
def
get_success_url
(
self
):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
article
.
id
})
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
object
.
id
})
class
DeleteView
(
generic
.
View
):
template_name
=
None
model
=
None
context_object_name
=
None
pk_url_kwarg
=
None
success_url
=
None
def
dispatch
(
self
,
request
:
HttpRequest
,
*
args
:
Any
,
**
kwargs
:
Any
)
->
HttpResponse
:
self
.
object
=
self
.
get_object
()
return
super
()
.
dispatch
(
request
,
*
args
,
**
kwargs
)
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
return
self
.
render_to_response
()
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
perform_delete
()
return
redirect
(
self
.
get_success_url
())
def
article_delete_view
(
request
,
id
):
get_object_or_404
(
Article
,
id
=
id
)
.
delete
()
def
perform_delete
(
self
):
self
.
object
.
delete
()
return
redirect
(
'index'
)
def
get_object
(
self
):
return
get_object_or_404
(
self
.
model
,
id
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
))
def
get_context_data
(
self
,
**
kwargs
):
kwargs
[
self
.
context_object_name
]
=
self
.
object
return
kwargs
def
get_success_url
(
self
):
return
self
.
success_url
def
render_to_response
(
self
,
**
kwargs
):
return
render
(
self
.
request
,
self
.
template_name
,
context
=
self
.
get_context_data
(
**
kwargs
),
)
class
ArticleDeleteView
(
generic
.
DeleteView
):
model
=
Article
pk_url_kwarg
=
'id'
success_url
=
reverse_lazy
(
'index'
)
ap_12/webapp/views/comments.py
View file @
3e240412
from
django.shortcuts
import
redirect
from
django.forms.models
import
BaseModelForm
from
django.http
import
HttpResponse
from
django.views
import
generic
from
django.urls
import
reverse
from
webapp.models
import
Comment
,
Article
from
webapp.models
import
Comment
from
webapp.forms
import
CommentForm
from
webapp.views.articles
import
UpdateView
,
DeleteView
class
CommentCreateView
(
generic
.
CreateView
):
...
...
@@ -21,3 +21,22 @@ class CommentCreateView(generic.CreateView):
comment
.
save
()
return
redirect
(
'articles_detail'
,
id
=
article_id
)
class
CommentUpdateView
(
generic
.
UpdateView
):
model
=
Comment
template_name
=
'comments/comment.html'
form_class
=
CommentForm
context_object_name
=
'comment'
pk_url_kwarg
=
'comment_id'
def
get_success_url
(
self
):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
object
.
article
.
id
})
class
CommentDeleteView
(
generic
.
DeleteView
):
model
=
Comment
pk_url_kwarg
=
'comment_id'
def
get_success_url
(
self
):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
object
.
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