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
ece54624
Commit
ece54624
authored
Jan 25, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 49
parent
ded8da55
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
12 deletions
+63
-12
.gitignore
.gitignore
+2
-1
urls.py
ap_12/core/urls.py
+3
-2
0003_auto_20240122_1347.py
ap_12/webapp/migrations/0003_auto_20240122_1347.py
+28
-0
models.py
ap_12/webapp/models.py
+13
-2
views.py
ap_12/webapp/views.py
+17
-7
No files found.
.gitignore
View file @
ece54624
venv
db.sqlite3
.idea
ap_12/db.sqlite3
\ No newline at end of file
ap_12/core/urls.py
View file @
ece54624
...
...
@@ -20,9 +20,10 @@ from webapp import views
urlpatterns
=
[
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
''
,
views
.
index_view
,
name
=
'index'
),
path
(
''
,
views
.
IndexRedirectView
.
as_view
(),
name
=
'redirect_to_index'
),
path
(
'articles'
,
views
.
IndexView
.
as_view
(),
name
=
'index'
),
path
(
'articles/add'
,
views
.
article_create_view
,
name
=
'articles_add'
),
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>/update'
,
views
.
article_update_view
,
name
=
'articles_update'
),
path
(
'articles/<int:id>/delete'
,
views
.
article_delete_view
,
name
=
'articles_delete'
),
]
ap_12/webapp/migrations/0003_auto_20240122_1347.py
0 → 100644
View file @
ece54624
# Generated by Django 3.2.19 on 2024-01-22 13:47
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'webapp'
,
'0002_auto_20240108_1448'
),
]
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
,
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
=
'webapp.article'
,
verbose_name
=
'Статья'
)),
],
),
migrations
.
DeleteModel
(
name
=
'User'
,
),
]
ap_12/webapp/models.py
View file @
ece54624
...
...
@@ -23,6 +23,17 @@ class Article(models.Model):
return
f
'{self.id} {self.title}'
class
Comment
(
models
.
Model
):
article
=
models
.
ForeignKey
(
'webapp.Article'
,
related_name
=
'comments'
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
'Статья'
)
text
=
models
.
TextField
(
max_length
=
400
,
verbose_name
=
'Текст'
)
author
=
models
.
CharField
(
max_length
=
40
,
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
User
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
200
,
verbose_name
=
'Имя'
)
def
__str__
(
self
):
return
self
.
text
[:
20
]
ap_12/webapp/views.py
View file @
ece54624
from
typing
import
Any
from
django.shortcuts
import
render
,
get_object_or_404
,
redirect
from
django.views
import
generic
from
webapp.forms
import
ArticleForm
from
webapp.models
import
Article
...
...
@@ -6,16 +9,23 @@ from webapp.models import Article
# CRUD - Create Read Update Delete
def
index_view
(
request
):
class
IndexRedirectView
(
generic
.
RedirectView
):
pattern_name
=
'index'
class
IndexView
(
generic
.
View
):
def
get
(
self
,
request
):
context
=
{
'articles'
:
Article
.
objects
.
all
()}
return
render
(
request
,
'index.html'
,
context
)
def
article_detail_view
(
request
,
id
):
context
=
{
'article'
:
get_object_or_404
(
Article
,
id
=
id
)}
class
ArticleDetailView
(
generic
.
TemplateView
):
template_name
=
'articles/detail.html'
return
render
(
request
,
'articles/detail.html'
,
context
)
def
get_context_data
(
self
,
**
kwargs
):
kwargs
[
'article'
]
=
get_object_or_404
(
Article
,
id
=
kwargs
[
'id'
])
return
super
()
.
get_context_data
(
**
kwargs
)
def
article_create_view
(
request
):
...
...
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