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
101e26bc
Commit
101e26bc
authored
Aug 30, 2021
by
Владислав Андреев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asdf
parent
4cda59c6
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
105 additions
and
46 deletions
+105
-46
views.py
articles/helpers/views.py
+47
-1
create.html
articles/templates/articles/create.html
+6
-6
detail.html
articles/templates/articles/detail.html
+10
-8
list.html
articles/templates/authors/list.html
+0
-4
create_cc.html
articles/templates/comments/create_cc.html
+9
-0
urls.py
articles/urls.py
+8
-8
views.py
articles/views.py
+25
-19
No files found.
articles/helpers/views.py
View file @
101e26bc
from
django.core.paginator
import
Paginator
,
PageNotAnInteger
,
EmptyPage
from
django.shortcuts
import
render
,
redirect
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.views
import
View
from
django.views.generic
import
TemplateView
,
ListView
from
django.views.generic.base
import
ContextMixin
class
CustomFormView
(
View
):
...
...
@@ -63,3 +64,48 @@ class ListView(TemplateView):
def
get_queryset
(
self
):
return
self
.
model
.
objects
.
all
()
class
DetailView
(
TemplateView
):
context_object_name
=
'objects'
model
=
None
key_kwarg
=
'pk'
def
get_context_data
(
self
,
**
kwargs
):
context
=
super
()
.
get_context_data
(
**
kwargs
)
context
[
self
.
context_object_name
]
=
self
.
get_object
()
return
context
def
get_object
(
self
):
pk
=
self
.
kwargs
.
get
(
self
.
key_kwarg
)
return
get_object_or_404
(
self
.
model
,
pk
=
pk
)
class
CreateView
(
View
):
form_class
=
None
template_name
=
None
model
=
None
redirect_url
=
None
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
self
.
form_class
()
context
=
{
'form'
:
form
}
return
render
(
request
=
request
,
template_name
=
self
.
template_name
,
context
=
context
)
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
self
.
form_class
(
data
=
request
.
POST
)
if
form
.
is_valid
():
return
self
.
form_valid
(
form
)
else
:
return
self
.
form_invalid
(
form
)
def
form_valid
(
self
,
form
):
self
.
object
=
form
.
save
()
return
redirect
(
self
.
get_redirect_url
())
def
form_invalid
(
self
,
form
):
context
=
{
'form'
:
form
}
return
render
(
request
=
self
.
request
,
template_name
=
self
.
template_name
,
context
=
context
)
def
get_redirect_url
(
self
):
return
self
.
redirect_url
\ No newline at end of file
articles/templates/articles/create.html
View file @
101e26bc
{% extends 'base.html' %}
{% block content %}
<form
action=
"{% url 'article_create' %}"
method=
"POST"
>
<div
class=
"container"
>
<h1>
Create Article
</h1>
<form
action=
"{% url 'article_create' %}"
method=
"POST"
>
{% csrf_token %}
{{ form.as_p }}
<input
type=
"submit"
value=
"create"
>
</form>
</div>
</form>
{% endblock %}
\ No newline at end of file
articles/templates/articles/detail.html
View file @
101e26bc
...
...
@@ -15,11 +15,13 @@
<li>
Автор: {{comment.author}}
</li>
<li>
Текст: {{comment.text}}
</li>
<li>
Дата создания: {{comment.created_at}}
</li>
<hr>
{% endfor %}
</ul>
</div>
<div>
<form
action=
"{% url 'add_comment' article.pk %}"
method=
"POST"
>
<form
action=
"{% url 'create_comment' article.pk %}"
method=
"POST"
>
{% csrf_token %}
{{ form.as_p }}
<input
type=
"submit"
...
...
articles/templates/authors/list.html
View file @
101e26bc
...
...
@@ -26,10 +26,6 @@
{% endfor %}
</tbody>
</table>
This text will be HTML-escaped, and will appear in all lowercase.
<br>
{% filter force_escape|upper %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}
<a
href=
"{% url 'author_create' %}"
>
Создать автора
</a>
{% include 'partial/pagination.html' %}
...
...
articles/templates/comments/create_cc.html
0 → 100644
View file @
101e26bc
{% extends 'base.html' %}
{% block content %}
<h1>
Create Comment
</h1>
<form
action=
"{% url 'add_comment' article.pk %} "
>
{{ form.as_p }}
</form>
{% endblock %}
\ No newline at end of file
articles/urls.py
View file @
101e26bc
from
django.urls
import
path
from
.views
import
(
ArticleListView
,
ArticleDetailView
,
CommentView
,
author_edit_view
,
author_delete_view
,
AuthorView
,
CreateArticleView
,
ArticleUpdateView
,
AuthorListView
ArticleDetailView
,
author_edit_view
,
author_delete_view
,
AuthorView
,
CreateArticleView
,
ArticleUpdateView
,
AuthorListView
,
ArticleCommentCreateView
)
urlpatterns
=
[
...
...
@@ -16,13 +17,12 @@ urlpatterns = [
ArticleDetailView
.
as_view
(),
name
=
"article_detail"
),
path
(
'article/<int:article_pk>/comment/add/'
,
CommentView
.
as_view
(),
name
=
"add_comment"
path
(
'articles/<int:pk>/comment/add'
,
ArticleCommentCreateView
.
as_view
(),
name
=
'create_comment'
),
path
(
'article/create
/
'
,
'article/create'
,
CreateArticleView
.
as_view
(),
name
=
'article_create'
),
...
...
articles/views.py
View file @
101e26bc
from
django.db.models
import
Q
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.template.defaultfilters
import
urlencode
from
django.urls
import
reverse
from
django.urls
import
reverse
,
reverse_lazy
from
django.views
import
View
from
django.views.generic
import
FormView
from
django.views.generic
import
FormView
,
DetailView
from
.helpers.views
import
CustomFormView
,
ListView
from
articles.models
import
Article
,
Author
from
.helpers.views
import
CustomFormView
,
ListView
,
CreateView
from
articles.models
import
Article
,
Author
,
Comment
from
.forms
import
AuthorForm
,
CommentForm
,
ArticleForm
,
SearchForm
...
...
@@ -58,24 +59,27 @@ class AuthorListView(ListView):
paginate_by
=
5
class
CreateArticleView
(
C
ustomForm
View
):
class
CreateArticleView
(
C
reate
View
):
template_name
=
'articles/create.html'
form_class
=
ArticleForm
model
=
Article
def
form_valid
(
self
,
form
):
data
=
{}
tags
=
form
.
cleaned_data
.
pop
(
'tags'
)
for
key
,
value
in
form
.
cleaned_data
.
items
():
if
value
is
not
None
:
data
[
key
]
=
value
def
get_redirect_url
(
self
):
return
reverse
(
'article_detail'
,
kwargs
=
{
'pk'
:
self
.
object
.
pk
})
self
.
article
=
Article
.
objects
.
create
(
**
data
)
self
.
article
.
tags
.
set
(
tags
)
class
ArticleCommentCreateView
(
CreateView
):
model
=
Comment
form_class
=
CommentForm
template_name
=
'articles/create.html'
def
form_valid
(
self
,
form
):
article
=
get_object_or_404
(
Article
,
pk
=
self
.
kwargs
.
get
(
'pk'
))
form
.
instance
.
article
=
article
return
super
()
.
form_valid
(
form
)
def
get_redirect_url
(
self
):
return
reverse
(
'article_
list'
)
return
reverse
(
'article_
detail'
,
kwargs
=
{
'pk'
:
self
.
object
.
article
.
pk
}
)
class
ArticleUpdateView
(
FormView
):
...
...
@@ -133,13 +137,15 @@ class AuthorView(View):
return
redirect
(
'author_list'
)
class
ArticleDetailView
(
View
):
class
ArticleDetailView
(
DetailView
):
template_name
=
'articles/detail.html'
context_object_name
=
'article'
model
=
Article
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
})
context
=
{
'form'
:
form
,
'article'
:
Article
.
objects
.
get
(
pk
=
kwargs
.
get
(
'pk'
))}
return
render
(
request
=
request
,
template_name
=
self
.
template_name
,
context
=
context
)
class
CommentView
(
View
):
...
...
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