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
8dcdb18e
Commit
8dcdb18e
authored
Jan 30, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 51
parent
244ea540
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
66 additions
and
55 deletions
+66
-55
urls.py
ap_12/core/urls.py
+2
-2
article.html
ap_12/templates/articles/article.html
+8
-0
create.html
ap_12/templates/articles/create.html
+0
-9
update.html
ap_12/templates/articles/update.html
+0
-9
form.html
ap_12/templates/partial/form.html
+1
-1
forms.py
ap_12/webapp/forms.py
+11
-12
models.py
ap_12/webapp/models.py
+4
-1
views.py
ap_12/webapp/views.py
+40
-21
No files found.
ap_12/core/urls.py
View file @
8dcdb18e
...
...
@@ -22,8 +22,8 @@ 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/add'
,
views
.
article_create_view
,
name
=
'articles_add'
),
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
.
article_update_view
,
name
=
'articles_update'
),
path
(
'articles/<int:id>/update'
,
views
.
ArticleUpdateView
.
as_view
()
,
name
=
'articles_update'
),
path
(
'articles/<int:id>/delete'
,
views
.
article_delete_view
,
name
=
'articles_delete'
),
]
ap_12/templates/articles/article.html
0 → 100644
View file @
8dcdb18e
{% extends 'base.html' %}
{% block content %}
<h1>
{{ title }}
</h1>
{% include 'partial/form.html' with request_method='post' %}
{% endblock %}
\ No newline at end of file
ap_12/templates/articles/create.html
deleted
100644 → 0
View file @
244ea540
{% extends 'base.html' %}
{% block content %}
<h1>
Create Article
</h1>
<form
action=
"{% url 'articles_add' %}"
method=
"post"
>
{% include 'partial/article_form.html' with btn_text='Create' %}
</form>
{% endblock %}
\ No newline at end of file
ap_12/templates/articles/update.html
deleted
100644 → 0
View file @
244ea540
{% extends 'base.html' %}
{% block content %}
<h1>
Update Article
</h1>
<form
action=
"{% url 'articles_update' id=article.id %}"
method=
"post"
>
{% include 'partial/article_form.html' %}
</form>
{% endblock %}
\ No newline at end of file
ap_12/templates/partial/
article_
form.html
→
ap_12/templates/partial/form.html
View file @
8dcdb18e
{% if not fields_only %}
<form
action=
"{{ action_url }}"
method=
"{{ request_method|default:'get' }}"
>
<form
method=
"{{ request_method|default:'get' }}"
>
{% csrf_token %}
{% endif %}
...
...
ap_12/webapp/forms.py
View file @
8dcdb18e
from
django
import
forms
from
django.forms
import
widgets
from
django.forms
import
ValidationError
from
webapp
import
models
class
ArticleForm
(
forms
.
Form
):
title
=
forms
.
CharField
(
max_length
=
200
,
required
=
True
,
label
=
'Title'
)
author
=
forms
.
CharField
(
max_length
=
40
,
required
=
True
,
label
=
'Author'
)
text
=
forms
.
CharField
(
max_length
=
3000
,
required
=
True
,
label
=
'Text'
,
widget
=
widgets
.
Textarea
,
)
tags
=
forms
.
ModelMultipleChoiceField
(
required
=
False
,
label
=
'Tags'
,
queryset
=
models
.
Tag
.
objects
.
all
(),
widget
=
widgets
.
CheckboxSelectMultiple
()
)
class
ArticleForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
models
.
Article
fields
=
(
'title'
,
'text'
,
'author'
,
'tags'
)
def
clean
(
self
):
if
self
.
data
[
'title'
]
==
self
.
data
[
'text'
]:
raise
ValidationError
(
'Text should not duplicate title'
)
return
super
()
.
clean
()
ap_12/webapp/models.py
View file @
8dcdb18e
from
django.db
import
models
from
django.core.validators
import
MinLengthValidator
class
Article
(
models
.
Model
):
title
=
models
.
CharField
(
max_length
=
200
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Заголовок'
,
max_length
=
200
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Заголовок'
,
validators
=
[
MinLengthValidator
(
12
)]
)
text
=
models
.
TextField
(
null
=
False
,
blank
=
False
,
verbose_name
=
'Текст'
,
...
...
ap_12/webapp/views.py
View file @
8dcdb18e
from
typing
import
Any
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
webapp.forms
import
ArticleForm
from
webapp.models
import
Article
# CRUD - Create Read Update Delete
class
IndexRedirectView
(
generic
.
RedirectView
):
pattern_name
=
'index'
...
...
@@ -28,28 +27,48 @@ class ArticleDetailView(generic.TemplateView):
return
super
()
.
get_context_data
(
**
kwargs
)
def
article_create_view
(
request
):
if
request
.
method
==
'GET'
:
form
=
ArticleForm
()
return
render
(
request
,
'articles/create.html'
,
context
=
{
'form'
:
form
})
class
ArticleCreateView
(
generic
.
FormView
):
template_name
=
'articles/article.html'
form_class
=
ArticleForm
elif
request
.
method
==
'POST'
:
form
=
ArticleForm
(
data
=
request
.
POST
)
def
form_valid
(
self
,
form
):
self
.
article
=
form
.
save
()
return
super
()
.
form_valid
(
form
)
if
form
.
is_valid
(
):
tags
=
form
.
cleaned_data
.
pop
(
'tags'
)
def
get_success_url
(
self
):
return
reverse
(
'articles_detail'
,
kwargs
=
{
'id'
:
self
.
article
.
id
}
)
article
=
Article
.
objects
.
create
(
**
form
.
cleaned_data
)
article
.
tags
.
set
(
tags
)
return
redirect
(
'articles_detail'
,
id
=
article
.
id
)
class
ArticleUpdateView
(
generic
.
FormView
):
template_name
=
'articles/article.html'
form_class
=
ArticleForm
else
:
return
render
(
request
,
'articles/create.html'
,
context
=
{
'form'
:
form
},
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
article
=
self
.
get_object
()
return
super
()
.
dispatch
(
request
,
*
args
,
**
kwargs
)
def
get_object
(
self
):
return
get_object_or_404
(
Article
,
id
=
self
.
kwargs
.
get
(
'id'
))
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
return
kwargs
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
})
def
article_update_view
(
request
,
id
):
article
=
get_object_or_404
(
Article
,
id
=
id
)
...
...
@@ -63,7 +82,7 @@ def article_update_view(request, id):
})
return
render
(
request
,
'articles/
updat
e.html'
,
request
,
'articles/
articl
e.html'
,
context
=
{
'article'
:
article
,
'form'
:
form
},
)
...
...
@@ -83,7 +102,7 @@ def article_update_view(request, id):
return
redirect
(
'index'
)
else
:
return
render
(
request
,
'articles/
updat
e.html'
,
context
=
{
'form'
:
form
,
'article'
:
article
})
return
render
(
request
,
'articles/
articl
e.html'
,
context
=
{
'form'
:
form
,
'article'
:
article
})
def
article_delete_view
(
request
,
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