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
244ea540
Commit
244ea540
authored
Jan 26, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lessn 50
parent
ece54624
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
177 additions
and
16 deletions
+177
-16
.gitignore
.gitignore
+1
-1
db.sqlite3
ap_12/db.sqlite3
+0
-0
styles.css
ap_12/static/css/styles.css
+0
-1
detail.html
ap_12/templates/articles/detail.html
+11
-0
index.html
ap_12/templates/index.html
+10
-7
forms.py
ap_12/webapp/forms.py
+6
-0
0004_auto_20240125_1342.py
ap_12/webapp/migrations/0004_auto_20240125_1342.py
+35
-0
0005_alter_article_tags.py
ap_12/webapp/migrations/0005_alter_article_tags.py
+18
-0
0006_rename_tags_article_tags_old.py
ap_12/webapp/migrations/0006_rename_tags_article_tags_old.py
+18
-0
0007_auto_20240125_1402.py
ap_12/webapp/migrations/0007_auto_20240125_1402.py
+23
-0
0008_auto_20240125_1404.py
ap_12/webapp/migrations/0008_auto_20240125_1404.py
+28
-0
models.py
ap_12/webapp/models.py
+16
-0
views.py
ap_12/webapp/views.py
+11
-7
No files found.
.gitignore
View file @
244ea540
venv
db.sqlite3
.idea
ap_12/db.sqlite3
\ No newline at end of file
*/db.sqlite3
\ No newline at end of file
ap_12/db.sqlite3
View file @
244ea540
No preview for this file type
ap_12/static/css/styles.css
View file @
244ea540
...
...
@@ -7,5 +7,4 @@ body {
.container
{
margin
:
0
auto
;
width
:
80%
;
text-align
:
center
;
}
ap_12/templates/articles/detail.html
View file @
244ea540
...
...
@@ -7,6 +7,17 @@
<p>
{{ article.text }}
</p>
<p>
By: {{ article.author }}
</p>
<h2>
Tags
</h2>
<ul
class=
"w-25"
>
{% for tag in article.tags.all %}
<li>
{{ tag.name }}
</li>
{% endfor %}
</ul>
<a
href=
"{% url 'articles_update' id=article.id %}"
class=
"btn btn-primary"
>
Update
</a>
...
...
ap_12/templates/index.html
View file @
244ea540
{% extends 'base.html' %}
{% block content %}
<div
class=
"text-center"
>
<h1>
Articles
</h1>
<p><a
href=
"{% url 'articles_add' %}"
>
Добавить
</a></p>
...
...
@@ -10,5 +11,7 @@
<a
href=
"{% url 'articles_detail' id=article.id %}"
>
Подробнее...
</a>
<hr>
{% endfor %}
</div>
{% endblock %}
\ No newline at end of file
ap_12/webapp/forms.py
View file @
244ea540
from
django
import
forms
from
django.forms
import
widgets
from
webapp
import
models
class
ArticleForm
(
forms
.
Form
):
title
=
forms
.
CharField
(
max_length
=
200
,
required
=
True
,
label
=
'Title'
)
...
...
@@ -9,3 +11,7 @@ class ArticleForm(forms.Form):
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
()
)
ap_12/webapp/migrations/0004_auto_20240125_1342.py
0 → 100644
View file @
244ea540
# Generated by Django 3.2.19 on 2024-01-25 13:42
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'webapp'
,
'0003_auto_20240122_1347'
),
]
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
=
'Тег'
)),
],
),
migrations
.
CreateModel
(
name
=
'ArticleTag'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
(
'article'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'article_tags'
,
to
=
'webapp.article'
)),
(
'tag'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'tag_articles'
,
to
=
'webapp.tag'
)),
],
),
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
related_name
=
'article'
,
through
=
'webapp.ArticleTag'
,
to
=
'webapp.Tag'
),
),
]
ap_12/webapp/migrations/0005_alter_article_tags.py
0 → 100644
View file @
244ea540
# Generated by Django 3.2.23 on 2024-01-25 13:48
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'webapp'
,
'0004_auto_20240125_1342'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
related_name
=
'articles'
,
through
=
'webapp.ArticleTag'
,
to
=
'webapp.Tag'
),
),
]
ap_12/webapp/migrations/0006_rename_tags_article_tags_old.py
0 → 100644
View file @
244ea540
# Generated by Django 3.2.19 on 2024-01-25 14:02
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'webapp'
,
'0005_alter_article_tags'
),
]
operations
=
[
migrations
.
RenameField
(
model_name
=
'article'
,
old_name
=
'tags'
,
new_name
=
'tags_old'
,
),
]
ap_12/webapp/migrations/0007_auto_20240125_1402.py
0 → 100644
View file @
244ea540
# Generated by Django 3.2.19 on 2024-01-25 14:02
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'webapp'
,
'0006_rename_tags_article_tags_old'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
related_name
=
'articles'
,
to
=
'webapp.Tag'
),
),
migrations
.
AlterField
(
model_name
=
'article'
,
name
=
'tags_old'
,
field
=
models
.
ManyToManyField
(
related_name
=
'articles_set'
,
through
=
'webapp.ArticleTag'
,
to
=
'webapp.Tag'
),
),
]
ap_12/webapp/migrations/0008_auto_20240125_1404.py
0 → 100644
View file @
244ea540
# Generated by Django 3.2.19 on 2024-01-25 14:04
from
django.db
import
migrations
def
transfer_tags
(
apps
,
schema_editor
):
Article
=
apps
.
get_model
(
'webapp.Article'
)
for
article
in
Article
.
objects
.
all
():
article
.
tags
.
set
(
article
.
tags_old
.
all
())
def
rollback_transfer
(
apps
,
schema_editor
):
Article
=
apps
.
get_model
(
'webapp.Article'
)
for
article
in
Article
.
objects
.
all
():
article
.
tags_old
.
set
(
article
.
tags
.
all
())
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'webapp'
,
'0007_auto_20240125_1402'
),
]
operations
=
[
migrations
.
RunPython
(
transfer_tags
,
rollback_transfer
)
]
ap_12/webapp/models.py
View file @
244ea540
...
...
@@ -15,6 +15,9 @@ class Article(models.Model):
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Дата создания'
)
updated_at
=
models
.
DateTimeField
(
auto_now
=
True
,
verbose_name
=
'Дата обновления'
)
tags_old
=
models
.
ManyToManyField
(
'webapp.Tag'
,
related_name
=
'articles_set'
,
through
=
'webapp.ArticleTag'
)
tags
=
models
.
ManyToManyField
(
'webapp.Tag'
,
related_name
=
'articles'
)
class
Meta
:
verbose_name
=
'статья'
verbose_name_plural
=
'статьи'
...
...
@@ -37,3 +40,16 @@ class Comment(models.Model):
def
__str__
(
self
):
return
self
.
text
[:
20
]
class
Tag
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
30
,
verbose_name
=
'Тег'
)
def
__str__
(
self
):
return
self
.
name
class
ArticleTag
(
models
.
Model
):
article
=
models
.
ForeignKey
(
'webapp.Article'
,
on_delete
=
models
.
CASCADE
,
related_name
=
'article_tags'
)
tag
=
models
.
ForeignKey
(
'webapp.Tag'
,
on_delete
=
models
.
CASCADE
,
related_name
=
'tag_articles'
)
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
)
ap_12/webapp/views.py
View file @
244ea540
...
...
@@ -34,15 +34,14 @@ def article_create_view(request):
return
render
(
request
,
'articles/create.html'
,
context
=
{
'form'
:
form
})
elif
request
.
method
==
'POST'
:
print
(
request
.
POST
)
form
=
ArticleForm
(
data
=
request
.
POST
)
if
form
.
is_valid
():
article
=
Article
.
objects
.
create
(
title
=
request
.
POST
.
get
(
'title'
),
text
=
request
.
POST
.
get
(
'text'
),
author
=
request
.
POST
.
get
(
'author'
),
)
tags
=
form
.
cleaned_data
.
pop
(
'tags'
)
article
=
Article
.
objects
.
create
(
**
form
.
cleaned_data
)
article
.
tags
.
set
(
tags
)
return
redirect
(
'articles_detail'
,
id
=
article
.
id
)
else
:
...
...
@@ -60,6 +59,7 @@ def article_update_view(request, id):
'title'
:
article
.
title
,
'text'
:
article
.
text
,
'author'
:
article
.
author
,
'tags'
:
article
.
tags
.
all
(),
})
return
render
(
...
...
@@ -71,12 +71,16 @@ def article_update_view(request, id):
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/update.html'
,
context
=
{
'form'
:
form
,
'article'
:
article
})
...
...
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