Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
article_proj
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
Владислав Андреев
article_proj
Commits
41987858
Commit
41987858
authored
Aug 15, 2021
by
Владислав Андреев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lesson 50
parent
e3d5c74a
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
301 additions
and
4 deletions
+301
-4
forms.py
articles/forms.py
+7
-1
0004_auto_20210812_1418.py
articles/migrations/0004_auto_20210812_1418.py
+40
-0
0005_alter_article_tags.py
articles/migrations/0005_alter_article_tags.py
+18
-0
0006_auto_20210812_1509.py
articles/migrations/0006_auto_20210812_1509.py
+22
-0
0007_auto_20210812_1511.py
articles/migrations/0007_auto_20210812_1511.py
+23
-0
0008_remove_article_tags_old.py
articles/migrations/0008_remove_article_tags_old.py
+17
-0
0009_article_tags_old.py
articles/migrations/0009_article_tags_old.py
+18
-0
0010_auto_20210812_1520.py
articles/migrations/0010_auto_20210812_1520.py
+25
-0
0011_auto_20210812_1522.py
articles/migrations/0011_auto_20210812_1522.py
+20
-0
models.py
articles/models.py
+42
-0
article_list.html
articles/templates/articles/article_list.html
+1
-1
detail.html
articles/templates/articles/detail.html
+32
-0
urls.py
articles/urls.py
+12
-1
views.py
articles/views.py
+24
-1
No files found.
articles/forms.py
View file @
41987858
from
django.forms.models
import
ModelForm
from
django.forms.models
import
ModelForm
from
articles.models
import
Article
,
Author
from
articles.models
import
Article
,
Author
,
Comment
class
AuthorForm
(
ModelForm
):
class
AuthorForm
(
ModelForm
):
class
Meta
:
class
Meta
:
model
=
Author
model
=
Author
fields
=
(
"name"
,)
fields
=
(
"name"
,)
class
CommentForm
(
ModelForm
):
class
Meta
:
model
=
Comment
fields
=
(
"text"
,
"author"
)
articles/migrations/0004_auto_20210812_1418.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 14:18
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0003_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
=
31
,
verbose_name
=
'Тег'
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Время создания'
)),
],
),
migrations
.
AlterField
(
model_name
=
'author'
,
name
=
'name'
,
field
=
models
.
CharField
(
max_length
=
50
,
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
,
related_name
=
'article_tags'
,
to
=
'articles.article'
,
verbose_name
=
'Статья'
)),
(
'tag'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
related_name
=
'tag_articles'
,
to
=
'articles.tag'
,
verbose_name
=
'Тег'
)),
],
),
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'tags'
,
through
=
'articles.ArticleTag'
,
to
=
'articles.Tag'
),
),
]
articles/migrations/0005_alter_article_tags.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 14:53
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0004_auto_20210812_1418'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'articles'
,
through
=
'articles.ArticleTag'
,
to
=
'articles.Tag'
),
),
]
articles/migrations/0006_auto_20210812_1509.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 15:09
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0005_alter_article_tags'
),
]
operations
=
[
migrations
.
RemoveField
(
model_name
=
'article'
,
name
=
'tags'
,
),
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags_old'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'articles'
,
to
=
'articles.Tag'
),
),
]
articles/migrations/0007_auto_20210812_1511.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 15:11
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0006_auto_20210812_1509'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'articles'
,
to
=
'articles.Tag'
),
),
migrations
.
AlterField
(
model_name
=
'article'
,
name
=
'tags_old'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'articles_old'
,
to
=
'articles.Tag'
),
),
]
articles/migrations/0008_remove_article_tags_old.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 15:20
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0007_auto_20210812_1511'
),
]
operations
=
[
migrations
.
RemoveField
(
model_name
=
'article'
,
name
=
'tags_old'
,
),
]
articles/migrations/0009_article_tags_old.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 15:20
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0008_remove_article_tags_old'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'article'
,
name
=
'tags_old'
,
field
=
models
.
ManyToManyField
(
blank
=
True
,
related_name
=
'articles_old'
,
through
=
'articles.ArticleTag'
,
to
=
'articles.Tag'
),
),
]
articles/migrations/0010_auto_20210812_1520.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 15:20
from
django.db
import
migrations
def
transfer_tags
(
apps
,
schema_editor
):
Article
=
apps
.
get_model
(
'articles.Article'
)
for
article
in
Article
.
objects
.
all
():
article
.
tags
.
set
(
article
.
tags_old
.
all
())
def
rollback_transfer
(
apps
,
schema_editor
):
Article
=
apps
.
get_model
(
'articles.Article'
)
for
article
in
Article
.
objects
.
all
():
article
.
tags_old
.
set
(
article
.
tags
.
all
())
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0009_article_tags_old'
),
]
operations
=
[
migrations
.
RunPython
(
transfer_tags
,
rollback_transfer
)
]
articles/migrations/0011_auto_20210812_1522.py
0 → 100644
View file @
41987858
# Generated by Django 3.2.6 on 2021-08-12 15:22
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'articles'
,
'0010_auto_20210812_1520'
),
]
operations
=
[
migrations
.
RemoveField
(
model_name
=
'article'
,
name
=
'tags_old'
,
),
migrations
.
DeleteModel
(
name
=
'ArticleTag'
,
),
]
articles/models.py
View file @
41987858
...
@@ -4,6 +4,9 @@ from django.db import models
...
@@ -4,6 +4,9 @@ from django.db import models
class
Author
(
models
.
Model
):
class
Author
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
,
null
=
False
,
verbose_name
=
"Автор"
)
name
=
models
.
CharField
(
max_length
=
50
,
null
=
False
,
verbose_name
=
"Автор"
)
def
__str__
(
self
):
return
self
.
name
class
Article
(
models
.
Model
):
class
Article
(
models
.
Model
):
title
=
models
.
CharField
(
max_length
=
250
,
null
=
False
)
title
=
models
.
CharField
(
max_length
=
250
,
null
=
False
)
...
@@ -12,6 +15,18 @@ class Article(models.Model):
...
@@ -12,6 +15,18 @@ class Article(models.Model):
"articles.Author"
,
"articles.Author"
,
on_delete
=
models
.
SET_NULL
,
on_delete
=
models
.
SET_NULL
,
null
=
True
,
related_name
=
'articles'
)
null
=
True
,
related_name
=
'articles'
)
tags_old
=
models
.
ManyToManyField
(
'articles.Tag'
,
related_name
=
"articles_old"
,
through
=
'articles.ArticleTag'
,
through_fields
=
(
'article'
,
'tag'
),
blank
=
True
)
tags
=
models
.
ManyToManyField
(
'articles.Tag'
,
related_name
=
"articles"
,
blank
=
True
)
class
Comment
(
models
.
Model
):
class
Comment
(
models
.
Model
):
...
@@ -27,3 +42,30 @@ class Comment(models.Model):
...
@@ -27,3 +42,30 @@ class Comment(models.Model):
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
text
[:
20
]
return
self
.
text
[:
20
]
class
Tag
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
31
,
verbose_name
=
'Тег'
)
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
'Время создания'
)
def
__str__
(
self
):
return
self
.
name
class
ArticleTag
(
models
.
Model
):
article
=
models
.
ForeignKey
(
'articles.Article'
,
related_name
=
'article_tags'
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
"Статья"
)
tag
=
models
.
ForeignKey
(
'articles.Tag'
,
related_name
=
'tag_articles'
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
"Тег"
)
def
__str__
(
self
):
return
f
"{self.article} | {self.tag}"
articles/templates/articles/article_list.html
View file @
41987858
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
{% for article in articles %}
{% for article in articles %}
<tr>
<tr>
<th
scope=
"row"
>
1
</th>
<th
scope=
"row"
>
1
</th>
<td>
{{ article.title }}
</td>
<td>
<a
href=
"{% url 'article_detail' article.pk %}"
>
{{ article.title }}
</a>
</td>
<td>
{{ article.author.name }}
</td>
<td>
{{ article.author.name }}
</td>
</tr>
</tr>
{% endfor %}
{% endfor %}
...
...
articles/templates/articles/detail.html
0 → 100644
View file @
41987858
{% extends "base.html" %}
{% block content %}
<div
class=
"container"
>
<div>
<h3>
{{ article.title }}
</h3>
<p>
{{ article.body }}
</p>
<p>
{{ article.author }}
</p>
</div>
<div>
<ul>
{% for comment in article.comments.all %}
<li>
Автор: {{comment.author}}
</li>
<li>
Текст: {{comment.text}}
</li>
<li>
Дата создания: {{comment.created_at}}
</li>
{% endfor %}
</ul>
</div>
<div>
<form
action=
"{% url 'add_comment' article.pk %}"
method=
"POST"
>
{% csrf_token %}
{{ form.as_p }}
<input
type=
"submit"
class=
"btn btn-success"
value=
"Оставить комментарий"
>
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
articles/urls.py
View file @
41987858
from
django.urls
import
path
from
django.urls
import
path
from
.views
import
(
from
.views
import
(
article_list_view
,
article_list_view
,
create_author_v
iew
,
ArticleDetailView
,
CommentV
iew
,
author_list_view
,
author_edit_view
,
author_delete_view
,
AuthorView
author_list_view
,
author_edit_view
,
author_delete_view
,
AuthorView
)
)
...
@@ -11,6 +11,17 @@ urlpatterns = [
...
@@ -11,6 +11,17 @@ urlpatterns = [
article_list_view
,
article_list_view
,
name
=
"article_list"
name
=
"article_list"
),
),
path
(
'article/<int:pk>/'
,
ArticleDetailView
.
as_view
(),
name
=
"article_detail"
),
path
(
'article/<int:article_pk>/comment/add/'
,
CommentView
.
as_view
(),
name
=
"add_comment"
),
path
(
path
(
'create/'
,
'create/'
,
AuthorView
.
as_view
(),
AuthorView
.
as_view
(),
...
...
articles/views.py
View file @
41987858
...
@@ -2,7 +2,7 @@ from django.shortcuts import render, redirect, get_object_or_404
...
@@ -2,7 +2,7 @@ from django.shortcuts import render, redirect, get_object_or_404
from
django.views
import
View
from
django.views
import
View
from
articles.models
import
Article
,
Author
from
articles.models
import
Article
,
Author
from
.forms
import
AuthorForm
from
.forms
import
AuthorForm
,
CommentForm
def
article_list_view
(
request
):
def
article_list_view
(
request
):
...
@@ -43,6 +43,29 @@ class AuthorView(View):
...
@@ -43,6 +43,29 @@ class AuthorView(View):
return
redirect
(
'author_list'
)
return
redirect
(
'author_list'
)
class
ArticleDetailView
(
View
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
CommentForm
()
return
render
(
request
,
"articles/detail.html"
,
context
=
{
'article'
:
get_object_or_404
(
Article
,
pk
=
kwargs
.
get
(
"pk"
)),
'form'
:
form
})
class
CommentView
(
View
):
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
CommentForm
(
request
.
POST
)
article_pk
=
kwargs
.
get
(
'article_pk'
)
if
form
.
is_valid
():
article
=
get_object_or_404
(
Article
,
pk
=
article_pk
)
article
.
comments
.
create
(
text
=
request
.
POST
.
get
(
"text"
),
author
=
request
.
POST
.
get
(
"author"
)
)
return
redirect
(
'article_detail'
,
article_pk
)
def
create_author_view
(
request
):
def
create_author_view
(
request
):
if
request
.
method
==
"GET"
:
if
request
.
method
==
"GET"
:
form
=
AuthorForm
()
form
=
AuthorForm
()
...
...
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