Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-11_http_framework
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
0
Merge Requests
0
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_http_framework
Commits
8592d5b4
Commit
8592d5b4
authored
Jul 10, 2023
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 49
parent
e48eda9f
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
233 additions
and
67 deletions
+233
-67
urls.py
core/urls.py
+3
-2
0006_comment.py
web/migrations/0006_comment.py
+29
-0
0007_auto_20230710_1741.py
web/migrations/0007_auto_20230710_1741.py
+38
-0
0008_rename_tags_article_tags_old.py
web/migrations/0008_rename_tags_article_tags_old.py
+18
-0
0009_auto_20230710_1813.py
web/migrations/0009_auto_20230710_1813.py
+24
-0
0010_auto_20230710_1814.py
web/migrations/0010_auto_20230710_1814.py
+26
-0
0011_auto_20230710_1818.py
web/migrations/0011_auto_20230710_1818.py
+20
-0
__init__.py
web/models/__init__.py
+3
-0
article.py
web/models/article.py
+2
-0
comment.py
web/models/comment.py
+46
-0
tag.py
web/models/tag.py
+9
-0
base.html
web/templates/base.html
+1
-1
views.py
web/views.py
+14
-64
No files found.
core/urls.py
View file @
8592d5b4
...
@@ -21,11 +21,12 @@ from web import views
...
@@ -21,11 +21,12 @@ from web import views
urlpatterns
=
[
urlpatterns
=
[
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
''
,
views
.
index_view
,
name
=
'main-page'
),
path
(
''
,
views
.
MainPageRedirectView
.
as_view
()),
path
(
'articles/'
,
views
.
index_view
,
name
=
'main_page'
),
path
(
'articles/add/'
,
views
.
ArticleCreateView
.
as_view
(),
name
=
'articles-add'
),
path
(
'articles/add/'
,
views
.
ArticleCreateView
.
as_view
(),
name
=
'articles-add'
),
# /articles/1/ OR /articles/?id=1
# /articles/1/ OR /articles/?id=1
# article_details_view(request, id)
# article_details_view(request, id)
path
(
'articles/<int:id>/'
,
views
.
article_detail_view
,
name
=
'articles-detail'
),
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
.
article_update_view
,
name
=
'article_update'
),
path
(
'articles/<int:id>/delete'
,
views
.
delete_article
,
name
=
'delete_article'
)
path
(
'articles/<int:id>/delete'
,
views
.
delete_article
,
name
=
'delete_article'
)
]
+
static
(
settings
.
MEDIA_URL
,
document_root
=
settings
.
MEDIA_ROOT
)
]
+
static
(
settings
.
MEDIA_URL
,
document_root
=
settings
.
MEDIA_ROOT
)
web/migrations/0006_comment.py
0 → 100644
View file @
8592d5b4
# Generated by Django 3.2.19 on 2023-07-10 10:25
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0005_alter_article_status'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Comment'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'text'
,
models
.
TextField
(
max_length
=
400
,
verbose_name
=
'Текст'
)),
(
'author'
,
models
.
CharField
(
blank
=
True
,
default
=
'Аноним'
,
max_length
=
40
,
null
=
True
,
verbose_name
=
'Автор'
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Время создания'
)),
(
'updated_at'
,
models
.
DateTimeField
(
auto_now
=
True
,
verbose_name
=
'Время обновления'
)),
(
'article'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'comments'
,
to
=
'web.article'
,
verbose_name
=
'Статья'
)),
],
options
=
{
'verbose_name'
:
'Комментарий'
,
'verbose_name_plural'
:
'Комментарии'
,
},
),
]
web/migrations/0007_auto_20230710_1741.py
0 → 100644
View file @
8592d5b4
# Generated by Django 3.2.19 on 2023-07-10 11:41
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0006_comment'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Tag'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
30
,
verbose_name
=
'Тег'
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Время создания'
)),
],
),
migrations
.
CreateModel
(
name
=
'ArticleTag'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'article'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
'web.article'
,
verbose_name
=
'Статья'
)),
(
'tag'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'articles'
,
to
=
'web.tag'
,
verbose_name
=
'Тег'
)),
],
options
=
{
'db_table'
:
'article_tag'
,
},
),
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
through
=
'web.ArticleTag'
,
to
=
'web.Tag'
),
),
]
web/migrations/0008_rename_tags_article_tags_old.py
0 → 100644
View file @
8592d5b4
# Generated by Django 3.2.19 on 2023-07-10 12:10
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0007_auto_20230710_1741'
),
]
operations
=
[
migrations
.
RenameField
(
model_name
=
'article'
,
old_name
=
'tags'
,
new_name
=
'tags_old'
,
),
]
web/migrations/0009_auto_20230710_1813.py
0 → 100644
View file @
8592d5b4
# Generated by Django 3.2.19 on 2023-07-10 12:13
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0008_rename_tags_article_tags_old'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'articles'
,
to
=
'web.Tag'
),
),
migrations
.
AlterField
(
model_name
=
'articletag'
,
name
=
'tag'
,
field
=
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'articles_set'
,
to
=
'web.tag'
,
verbose_name
=
'Тег'
),
),
]
web/migrations/0010_auto_20230710_1814.py
0 → 100644
View file @
8592d5b4
# Generated by Django 3.2.19 on 2023-07-10 12:14
from
django.db
import
migrations
def
transfer_tags
(
apps
,
schema_editor
):
Article
=
apps
.
get_model
(
'web.Article'
)
for
article
in
Article
.
objects
.
all
():
article
.
tags
.
set
(
article
.
tags_old
.
all
())
def
rollback_transfer
(
apps
,
schema_editor
):
Article
=
apps
.
get_model
(
'web.Article'
)
for
article
in
Article
.
objects
.
all
():
article
.
tags_old
.
set
(
article
.
tags
.
all
())
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0009_auto_20230710_1813'
),
]
operations
=
[
migrations
.
RunPython
(
transfer_tags
,
rollback_transfer
)
]
web/migrations/0011_auto_20230710_1818.py
0 → 100644
View file @
8592d5b4
# Generated by Django 3.2.19 on 2023-07-10 12:18
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'web'
,
'0010_auto_20230710_1814'
),
]
operations
=
[
migrations
.
RemoveField
(
model_name
=
'article'
,
name
=
'tags_old'
,
),
migrations
.
DeleteModel
(
name
=
'ArticleTag'
,
),
]
web/models/__init__.py
0 → 100644
View file @
8592d5b4
from
web.models.article
import
Article
,
StatusChoices
from
web.models.comment
import
Comment
from
web.models.tag
import
Tag
web/models.py
→
web/models
/article
.py
View file @
8592d5b4
...
@@ -18,6 +18,8 @@ class Article(models.Model):
...
@@ -18,6 +18,8 @@ class Article(models.Model):
verbose_name
=
'Заголовок'
verbose_name
=
'Заголовок'
)
)
tags
=
models
.
ManyToManyField
(
'web.Tag'
,
related_name
=
'articles'
,
blank
=
True
)
# accepted, check, unaccepted
# accepted, check, unaccepted
status
=
models
.
CharField
(
status
=
models
.
CharField
(
max_length
=
100
,
max_length
=
100
,
...
...
web/models/comment.py
0 → 100644
View file @
8592d5b4
from
django.db
import
models
# article_1 - (comment_1, comment_2)
# comment_1 = comment_1.article
# comment_1 = comment_1.article_id
# article_1 = article_1.comment.all()
class
Comment
(
models
.
Model
):
# article - object
# article_id - id
article
=
models
.
ForeignKey
(
'web.Article'
,
on_delete
=
models
.
CASCADE
,
related_name
=
'comments'
,
verbose_name
=
'Статья'
)
text
=
models
.
TextField
(
max_length
=
400
,
verbose_name
=
'Текст'
)
author
=
models
.
CharField
(
max_length
=
40
,
null
=
True
,
blank
=
True
,
default
=
'Аноним'
,
verbose_name
=
'Автор'
)
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Время создания'
)
updated_at
=
models
.
DateTimeField
(
auto_now
=
True
,
verbose_name
=
'Время обновления'
)
class
Meta
:
verbose_name
=
'Комментарий'
verbose_name_plural
=
'Комментарии'
def
__str__
(
self
):
return
f
'{self.article.title}: {self.author} | {self.text[:30]}...'
web/models/tag.py
0 → 100644
View file @
8592d5b4
from
django.db
import
models
class
Tag
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
30
,
verbose_name
=
'Тег'
)
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Время создания'
)
def
__str__
(
self
):
return
self
.
name
web/templates/base.html
View file @
8592d5b4
...
@@ -13,7 +13,7 @@
...
@@ -13,7 +13,7 @@
<body>
<body>
<nav
class=
"navbar navbar-expand-lg navbar-light bg-light"
>
<nav
class=
"navbar navbar-expand-lg navbar-light bg-light"
>
<div
class=
"container"
>
<div
class=
"container"
>
<a
class=
"navbar-brand"
href=
"{% url 'main
-
page' %}"
>
Articles
</a>
<a
class=
"navbar-brand"
href=
"{% url 'main
_
page' %}"
>
Articles
</a>
<button
class=
"navbar-toggler"
type=
"button"
data-bs-toggle=
"collapse"
data-bs-target=
"#navbarNav"
aria-controls=
"navbarNav"
aria-expanded=
"false"
aria-label=
"Toggle navigation"
>
<button
class=
"navbar-toggler"
type=
"button"
data-bs-toggle=
"collapse"
data-bs-target=
"#navbarNav"
aria-controls=
"navbarNav"
aria-expanded=
"false"
aria-label=
"Toggle navigation"
>
<span
class=
"navbar-toggler-icon"
></span>
<span
class=
"navbar-toggler-icon"
></span>
</button>
</button>
...
...
web/views.py
View file @
8592d5b4
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.urls
import
reverse
from
django.urls
import
reverse
from
django.views
import
View
from
django.views
.generic
import
View
,
TemplateView
,
Redirect
View
from
web.forms
import
ArticleForm
from
web.forms
import
ArticleForm
from
web.models
import
Article
,
StatusChoices
from
web.models
import
Article
,
StatusChoices
...
@@ -14,6 +14,10 @@ def index_view(request):
...
@@ -14,6 +14,10 @@ def index_view(request):
)
)
class
MainPageRedirectView
(
RedirectView
):
pattern_name
=
'main_page'
# Class-based views
# Class-based views
class
ArticleCreateView
(
View
):
class
ArticleCreateView
(
View
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
...
@@ -44,73 +48,19 @@ class ArticleCreateView(View):
...
@@ -44,73 +48,19 @@ class ArticleCreateView(View):
return
render
(
self
.
request
,
'article_create.html'
,
context
=
{
'form'
:
form
,
'status_choices'
:
StatusChoices
.
choices
})
return
render
(
self
.
request
,
'article_create.html'
,
context
=
{
'form'
:
form
,
'status_choices'
:
StatusChoices
.
choices
})
# title = request.POST.get('title')
# if not title:
# errors['title'] = 'Title should not be empty!'
# elif len(title) > 200:
# errors['title'] = 'Title should be 200 symbols or less'
# author = request.POST.get('author')
# if not author:
# errors['author'] = 'Author should not be empty!'
# elif len(author) > 40:
# errors['author'] = 'Author should be 40 symbols or less'
# text = request.POST.get('text')
# if not text:
# errors['text'] = 'Text should not be empty!'
# elif len(text) > 3000:
# errors['text'] = 'Text should be 3000 symbols or less'
# status = request.POST.get('status')
# if not status:
# errors['status'] = 'Status should not be empty!'
# if len(errors) > 0:
# article_not_saved = Article(title=title, status=status, text=text, author=author)
# return render(self.request,
# 'article_create.html',
# context={
# 'status_choices': StatusChoices.choices,
# 'errors': errors,
# 'article': article_not_saved
# })
article
=
Article
.
objects
.
create
(
title
=
title
,
author
=
author
,
status
=
status
,
text
=
text
)
return
redirect
(
'articles-detail'
,
id
=
article
.
id
)
# def article_create_view(request):
# if request.method == 'GET':
# return render(
# request,
# 'article_create.html',
# context={
# 'status_choices': StatusChoices.choices
# }
# )
#
# elif request.method == 'POST':
# data = request.POST
# article_data = {
# 'title': data.get('title'),
# 'text': data.get('text'),
# 'author': data.get('author'),
# 'status': data.get('status')
# }
#
# article = Article.objects.create(**article_data)
#
# return redirect('articles-detail', id=article.id)
# /articles/{id}
# /articles/{id}
def
article_detail_view
(
request
,
id
:
int
):
def
article_detail_view
(
request
,
id
:
int
):
article
=
get_object_or_404
(
Article
,
id
=
id
)
article
=
get_object_or_404
(
Article
,
id
=
id
)
return
render
(
request
,
'article.html'
,
context
=
{
'article'
:
article
})
return
render
(
request
,
'article.html'
,
context
=
{
'article'
:
article
})
class
ArticleDetailView
(
TemplateView
):
template_name
=
'article.html'
def
get_context_data
(
self
,
**
kwargs
):
article
=
get_object_or_404
(
Article
,
id
=
kwargs
.
get
(
'id'
))
return
super
()
.
get_context_data
(
article
=
article
)
def
article_update_view
(
request
,
id
):
def
article_update_view
(
request
,
id
):
article
=
get_object_or_404
(
Article
,
pk
=
id
)
article
=
get_object_or_404
(
Article
,
pk
=
id
)
if
request
.
method
==
'GET'
:
if
request
.
method
==
'GET'
:
...
@@ -132,7 +82,7 @@ def article_update_view(request, id):
...
@@ -132,7 +82,7 @@ def article_update_view(request, id):
article
.
save
()
article
.
save
()
return
redirect
(
'articles-detail'
,
id
=
article
.
pk
)
return
redirect
(
'articles-detail'
,
id
=
article
.
pk
)
else
:
else
:
return
render
(
request
,
'update.html'
,
context
=
{
'article'
:
article
,
'form'
:
form
,
'status_choices'
:
StatusChoices
.
choices
})
)
return
render
(
request
,
'update.html'
,
context
=
{
'article'
:
article
,
'form'
:
form
,
'status_choices'
:
StatusChoices
.
choices
})
def
delete_article
(
request
,
id
):
def
delete_article
(
request
,
id
):
...
@@ -142,4 +92,4 @@ def delete_article(request, id):
...
@@ -142,4 +92,4 @@ def delete_article(request, id):
elif
request
.
method
==
'POST'
:
elif
request
.
method
==
'POST'
:
article
.
delete
()
article
.
delete
()
return
redirect
(
'main
-
page'
)
return
redirect
(
'main
_
page'
)
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