Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-11_django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
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-11_django
Commits
a8300edd
Commit
a8300edd
authored
Jul 13, 2023
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 50
parent
d1ebeea8
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
74 additions
and
4 deletions
+74
-4
urls.py
core/urls.py
+1
-1
validators.py
core/validators.py
+10
-0
reqs.txt
reqs.txt
+1
-0
forms.py
web/forms.py
+9
-1
0012_alter_article_title.py
web/migrations/0012_alter_article_title.py
+19
-0
article.py
web/models/article.py
+5
-1
views.py
web/views.py
+29
-1
No files found.
core/urls.py
View file @
a8300edd
...
...
@@ -27,6 +27,6 @@ urlpatterns = [
# /articles/1/ OR /articles/?id=1
# article_details_view(request, id)
path
(
'articles/<int:id>/'
,
views
.
ArticleDetailView
.
as_view
(),
name
=
'articles-detail'
),
path
(
'articles/<int:id>/edit'
,
views
.
article_update_view
,
name
=
'article_update'
),
path
(
'articles/<int:id>/edit'
,
views
.
ArticleUpdateView
.
as_view
()
,
name
=
'article_update'
),
path
(
'articles/<int:id>/delete'
,
views
.
delete_article
,
name
=
'delete_article'
)
]
+
static
(
settings
.
MEDIA_URL
,
document_root
=
settings
.
MEDIA_ROOT
)
core/validators.py
0 → 100644
View file @
a8300edd
from
django.forms
import
ValidationError
def
at_least_5
(
v
):
if
len
(
v
)
<
5
:
raise
ValidationError
(
'This field should be at least
%(length)
d symbols long'
,
code
=
'too_short'
,
params
=
{
'length'
:
5
}
)
reqs.txt
View file @
a8300edd
asgiref==3.7.2
Django==3.2.19
django-extensions==3.2.3
pytz==2023.3
sqlparse==0.4.4
web/forms.py
View file @
a8300edd
from
django
import
forms
from
django.forms
import
widgets
from
django.forms
import
widgets
,
ValidationError
from
core.validators
import
at_least_5
from
web.models
import
Article
...
...
@@ -22,3 +23,10 @@ class ArticleModelForm(forms.ModelForm):
'text'
:
forms
.
Textarea
(
attrs
=
{
'class'
:
'form-control mb-3'
}),
'tags'
:
forms
.
CheckboxSelectMultiple
(
attrs
=
{
'class'
:
'mb-3'
})
}
def
clean
(
self
):
cleaned_data
=
super
()
.
clean
()
if
cleaned_data
.
get
(
'text'
)
==
cleaned_data
.
get
(
'title'
):
raise
ValidationError
(
'Text of the article should not duplicate it
\'
s title'
)
return
cleaned_data
web/migrations/0012_alter_article_title.py
0 → 100644
View file @
a8300edd
# Generated by Django 3.2.19 on 2023-07-13 11:12
import
core.validators
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0011_auto_20230710_1818'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'article'
,
name
=
'title'
,
field
=
models
.
CharField
(
max_length
=
200
,
validators
=
[
core
.
validators
.
at_least_5
],
verbose_name
=
'Заголовок'
),
),
]
web/models/article.py
View file @
a8300edd
from
django.db
import
models
from
django.db.models
import
TextChoices
from
django.core.validators
import
MinLengthValidator
class
StatusChoices
(
TextChoices
):
...
...
@@ -15,7 +16,10 @@ class Article(models.Model):
max_length
=
200
,
null
=
False
,
# <null>
blank
=
False
,
# ''
verbose_name
=
'Заголовок'
verbose_name
=
'Заголовок'
,
validators
=
[
MinLengthValidator
(
5
)
]
)
tags
=
models
.
ManyToManyField
(
'web.Tag'
,
related_name
=
'articles'
,
blank
=
True
)
...
...
web/views.py
View file @
a8300edd
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.urls
import
reverse
from
django.views.generic
import
View
,
TemplateView
,
RedirectView
from
django.views.generic
import
View
,
TemplateView
,
RedirectView
,
FormView
from
web.forms
import
ArticleForm
,
ArticleModelForm
from
web.models
import
Article
,
StatusChoices
...
...
@@ -56,6 +56,34 @@ class ArticleDetailView(TemplateView):
article
=
get_object_or_404
(
Article
,
id
=
kwargs
.
get
(
'id'
))
return
super
()
.
get_context_data
(
article
=
article
)
class
ArticleUpdateView
(
FormView
):
template_name
=
'update.html'
form_class
=
ArticleModelForm
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
article
=
get_object_or_404
(
self
.
form_class
.
Meta
.
model
,
id
=
kwargs
.
get
(
'id'
))
return
super
()
.
dispatch
(
request
,
*
args
,
**
kwargs
)
def
get_context_data
(
self
,
**
kwargs
):
return
super
()
.
get_context_data
(
article
=
self
.
article
,
**
kwargs
)
def
get_initial
(
self
):
initial
=
self
.
article
.
__dict__
initial
[
'tags'
]
=
self
.
article
.
tags
.
all
()
return
initial
def
get_form_kwargs
(
self
):
return
super
()
.
get_form_kwargs
()
|
{
'instance'
:
self
.
article
}
def
form_valid
(
self
,
form
):
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
,
pk
=
id
)
if
request
.
method
==
'GET'
:
...
...
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