Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
lesson_34
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
Владислав Андреев
lesson_34
Commits
8b95034d
Commit
8b95034d
authored
Jun 27, 2021
by
Владислав Андреев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asdfasd
parent
7d4e2da2
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
121 additions
and
10 deletions
+121
-10
0003_auto_20210624_2001.py
article/migrations/0003_auto_20210624_2001.py
+27
-0
models.py
article/models.py
+10
-4
urls.py
article/urls.py
+10
-1
views.py
article/views.py
+24
-3
create.html
templates/article/create.html
+6
-2
create.html
templates/author/create.html
+25
-0
list.html
templates/author/list.html
+16
-0
base.html
templates/base.html
+3
-0
No files found.
article/migrations/0003_auto_20210624_2001.py
0 → 100644
View file @
8b95034d
# Generated by Django 3.2.4 on 2021-06-24 14:01
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'article'
,
'0002_auto_20210623_1935'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Author'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'name'
,
models
.
CharField
(
max_length
=
100
,
verbose_name
=
'Имя'
)),
(
'last_name'
,
models
.
CharField
(
max_length
=
100
,
verbose_name
=
'Название'
)),
],
),
migrations
.
AlterField
(
model_name
=
'article'
,
name
=
'author'
,
field
=
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
'article.author'
,
verbose_name
=
'Автор'
),
),
]
article/models.py
View file @
8b95034d
from
django.db
import
models
class
Author
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Имя'
)
last_name
=
models
.
CharField
(
max_length
=
100
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Название'
)
def
__str__
(
self
):
return
f
"{self.name} {self.last_name}"
class
Article
(
models
.
Model
):
title
=
models
.
CharField
(
max_length
=
100
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Название'
)
content
=
models
.
TextField
(
max_length
=
3000
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Тело'
)
author
=
models
.
CharField
(
max_length
=
100
,
null
=
False
,
blank
=
False
,
verbose_name
=
'Автор'
)
author
=
models
.
ForeignKey
(
to
=
Author
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
'Автор'
)
created_at
=
models
.
DateTimeField
(
auto_now_add
=
True
,
verbose_name
=
"Дата и время создания"
)
updated_at
=
models
.
DateTimeField
(
auto_now
=
True
,
verbose_name
=
"Дата и время изменения"
)
publication_date
=
models
.
DateTimeField
(
verbose_name
=
"Дата и время публикации"
,
null
=
True
,
blank
=
False
)
...
...
@@ -13,6 +20,5 @@ class Article(models.Model):
return
f
"{self.pk}. {self.title}"
class
Meta
:
verbose_name
=
'Статья'
verbose_name_plural
=
'Статьи'
verbose_name
=
'Статья'
verbose_name_plural
=
'Статьи'
article/urls.py
View file @
8b95034d
from
django.urls
import
path
from
article.views
import
article_create_view
,
article_list_view
,
article_detail_view
,
article_update_view
from
article.views
import
\
article_create_view
,
\
article_list_view
,
\
article_detail_view
,
\
article_update_view
,
\
author_list_view
,
\
author_create_view
urlpatterns
=
[
...
...
@@ -7,4 +13,7 @@ urlpatterns = [
path
(
'list/'
,
article_list_view
,
name
=
'article_list'
),
path
(
'detail/<int:pk>'
,
article_detail_view
,
name
=
'article_detail'
),
path
(
'edit/<int:pk>'
,
article_update_view
,
name
=
'article_update'
),
path
(
'author/create'
,
author_create_view
,
name
=
"add_author"
),
path
(
'author/list'
,
author_list_view
,
name
=
"list_author"
)
]
\ No newline at end of file
article/views.py
View file @
8b95034d
from
django.shortcuts
import
render
from
article.models
import
Article
from
article.models
import
Article
,
Author
from
django.shortcuts
import
redirect
from
django.http
import
HttpResponseNotFound
from
datetime
import
datetime
...
...
@@ -11,17 +11,20 @@ def index_view(request):
def
article_create_view
(
request
):
if
request
.
method
==
'GET'
:
return
render
(
request
,
'article/create.html'
)
return
render
(
request
,
'article/create.html'
,
context
=
{
'authors'
:
Author
.
objects
.
all
()
})
elif
request
.
method
==
'POST'
:
publication_date
=
datetime
.
strptime
(
request
.
POST
.
get
(
"publication_date"
),
"
%
Y-
%
m-
%
dT
%
H:
%
M"
)
now_time
=
datetime
.
now
()
if
publication_date
<
datetime
.
now
():
return
render
(
request
,
'article/create.html'
,
context
=
{
"error_message"
:
f
"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"
})
author
=
Author
.
objects
.
get
(
id
=
request
.
POST
.
get
(
'author_id'
))
new_article
=
Article
.
objects
.
create
(
title
=
request
.
POST
.
get
(
'title'
),
content
=
request
.
POST
.
get
(
'content'
),
author
=
request
.
POST
.
get
(
'author'
)
,
author
=
author
,
publication_date
=
publication_date
)
new_article
.
save
()
...
...
@@ -63,3 +66,21 @@ def article_list_view(request):
return
render
(
request
,
'article/list.html'
,
context
=
{
'articles'
:
Article
.
objects
.
all
()
})
def
author_create_view
(
request
):
if
request
.
method
==
"GET"
:
return
render
(
request
,
'author/create.html'
)
if
request
.
method
==
"POST"
:
new_author
=
Author
.
objects
.
create
(
last_name
=
request
.
POST
.
get
(
"last_name"
),
name
=
request
.
POST
.
get
(
"name"
),
)
new_author
.
save
()
return
redirect
(
"list_author"
)
def
author_list_view
(
request
):
return
render
(
request
,
"author/list.html"
,
context
=
{
"authors"
:
Author
.
objects
.
all
()})
templates/article/create.html
View file @
8b95034d
...
...
@@ -20,7 +20,11 @@
</div>
<div
class=
"mb-3"
>
<label
for=
"author_input"
class=
"form-label"
>
Author
</label>
<input
type=
"text"
name=
"author"
class=
"form-control"
id=
"author_input"
placeholder=
"Автор..."
>
<select
name=
"author_id"
id=
"author_input"
>
{% for author in authors %}
<option
value=
"{{ author.id }}"
>
{{ author }}
</option>
{% endfor %}
</select>
</div>
<div
class=
"mb-3"
>
<label
for=
"publication_date_input"
class=
"form-label"
>
Дата публикации
</label>
...
...
templates/author/create.html
0 → 100644
View file @
8b95034d
{% extends 'base.html' %}
{% block content %}
<div
class=
"row"
>
<div
class=
"col-md-6"
>
{% if error_message %}
<div
class=
"alert alert-danger"
role=
"alert"
>
{{ error_message }}
</div>
{% endif %}
<form
action=
"{% url 'add_author' %}"
method=
"post"
>
{% csrf_token %}
<div
class=
"mb-3"
>
<label
for=
"name_input"
class=
"form-label"
>
Имя
</label>
<input
type=
"text"
name=
"name"
class=
"form-control"
id=
"name_input"
placeholder=
"Имя..."
>
</div>
<div
class=
"mb-3"
>
<label
for=
"last_name_input"
class=
"form-label"
>
Фамилия
</label>
<input
type=
"text"
name=
"last_name"
class=
"form-control"
id=
"last_name_input"
placeholder=
"Фамили..."
>
</div>
<button
type=
"submit"
class=
"btn btn-success"
>
Create
</button>
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
templates/author/list.html
0 → 100644
View file @
8b95034d
{% extends 'base.html' %}
{% block content %}
<div
class=
"row"
>
<div
class=
"col-md-6"
>
<div
class=
"list-group"
>
{% for author in authors %}
<div>
{{ author }}
</div>
{% endfor %}
</div>
</div>
</div>
<a
href=
"{% url 'add_author' %}"
class=
"btn btn-info"
>
Add New Author
</a>
{% endblock %}
templates/base.html
View file @
8b95034d
...
...
@@ -25,6 +25,9 @@
<li
class=
"nav-item"
>
<a
class=
"nav-link"
href=
"{% url 'add_article' %}"
>
Add New Article
</a>
</li>
<li
class=
"nav-item"
>
<a
class=
"nav-link"
href=
"{% url 'add_author' %}"
>
Add New Author
</a>
</li>
</ul>
</div>
</div>
...
...
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