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.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
import
View
from
django.views.generic
import
TemplateView
,
ListView
from
django.views.generic
import
TemplateView
,
ListView
from
django.views.generic.base
import
ContextMixin
class
CustomFormView
(
View
):
class
CustomFormView
(
View
):
...
@@ -63,3 +64,48 @@ class ListView(TemplateView):
...
@@ -63,3 +64,48 @@ class ListView(TemplateView):
def
get_queryset
(
self
):
def
get_queryset
(
self
):
return
self
.
model
.
objects
.
all
()
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' %}
{% extends 'base.html' %}
{% block content %}
{% block content %}
<form
action=
"{% url 'article_create' %}"
method=
"POST"
>
<div
class=
"container"
>
<div
class=
"container"
>
<h1>
Create Article
</h1>
<form
action=
"{% url 'article_create' %}"
method=
"POST"
>
{% csrf_token %}
{% csrf_token %}
{{ form.as_p }}
{{ form.as_p }}
<input
type=
"submit"
value=
"create"
>
<input
type=
"submit"
value=
"create"
>
</form>
</div>
</div>
</form>
{% endblock %}
{% endblock %}
\ No newline at end of file
articles/templates/articles/detail.html
View file @
101e26bc
...
@@ -15,11 +15,13 @@
...
@@ -15,11 +15,13 @@
<li>
Автор: {{comment.author}}
</li>
<li>
Автор: {{comment.author}}
</li>
<li>
Текст: {{comment.text}}
</li>
<li>
Текст: {{comment.text}}
</li>
<li>
Дата создания: {{comment.created_at}}
</li>
<li>
Дата создания: {{comment.created_at}}
</li>
<hr>
{% endfor %}
{% endfor %}
</ul>
</ul>
</div>
</div>
<div>
<div>
<form
action=
"{% url 'add_comment' article.pk %}"
method=
"POST"
>
<form
action=
"{% url 'create_comment' article.pk %}"
method=
"POST"
>
{% csrf_token %}
{% csrf_token %}
{{ form.as_p }}
{{ form.as_p }}
<input
type=
"submit"
<input
type=
"submit"
...
...
articles/templates/authors/list.html
View file @
101e26bc
...
@@ -26,10 +26,6 @@
...
@@ -26,10 +26,6 @@
{% endfor %}
{% endfor %}
</tbody>
</tbody>
</table>
</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>
<a
href=
"{% url 'author_create' %}"
>
Создать автора
</a>
{% include 'partial/pagination.html' %}
{% 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
django.urls
import
path
from
.views
import
(
from
.views
import
(
ArticleListView
,
ArticleListView
,
ArticleDetailView
,
CommentView
,
ArticleDetailView
,
author_edit_view
,
author_delete_view
,
AuthorView
,
CreateArticleView
,
ArticleUpdateView
,
AuthorListView
author_edit_view
,
author_delete_view
,
AuthorView
,
CreateArticleView
,
ArticleUpdateView
,
AuthorListView
,
ArticleCommentCreateView
)
)
urlpatterns
=
[
urlpatterns
=
[
...
@@ -16,13 +17,12 @@ urlpatterns = [
...
@@ -16,13 +17,12 @@ urlpatterns = [
ArticleDetailView
.
as_view
(),
ArticleDetailView
.
as_view
(),
name
=
"article_detail"
name
=
"article_detail"
),
),
path
(
path
(
'articles/<int:pk>/comment/add'
,
'article/<int:article_pk>/comment/add/'
,
ArticleCommentCreateView
.
as_view
(),
CommentView
.
as_view
(),
name
=
'create_comment'
name
=
"add_comment"
),
),
path
(
path
(
'article/create
/
'
,
'article/create'
,
CreateArticleView
.
as_view
(),
CreateArticleView
.
as_view
(),
name
=
'article_create'
name
=
'article_create'
),
),
...
...
articles/views.py
View file @
101e26bc
from
django.db.models
import
Q
from
django.db.models
import
Q
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.template.defaultfilters
import
urlencode
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
import
View
from
django.views.generic
import
FormView
from
django.views.generic
import
FormView
,
DetailView
from
.helpers.views
import
CustomFormView
,
ListView
from
.helpers.views
import
CustomFormView
,
ListView
,
CreateView
from
articles.models
import
Article
,
Author
from
articles.models
import
Article
,
Author
,
Comment
from
.forms
import
AuthorForm
,
CommentForm
,
ArticleForm
,
SearchForm
from
.forms
import
AuthorForm
,
CommentForm
,
ArticleForm
,
SearchForm
...
@@ -58,24 +59,27 @@ class AuthorListView(ListView):
...
@@ -58,24 +59,27 @@ class AuthorListView(ListView):
paginate_by
=
5
paginate_by
=
5
class
CreateArticleView
(
C
ustomForm
View
):
class
CreateArticleView
(
C
reate
View
):
template_name
=
'articles/create.html'
template_name
=
'articles/create.html'
form_class
=
ArticleForm
form_class
=
ArticleForm
model
=
Article
def
form_valid
(
self
,
form
):
def
get_redirect_url
(
self
):
data
=
{}
return
reverse
(
'article_detail'
,
kwargs
=
{
'pk'
:
self
.
object
.
pk
})
tags
=
form
.
cleaned_data
.
pop
(
'tags'
)
for
key
,
value
in
form
.
cleaned_data
.
items
():
if
value
is
not
None
:
data
[
key
]
=
value
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
)
return
super
()
.
form_valid
(
form
)
def
get_redirect_url
(
self
):
def
get_redirect_url
(
self
):
return
reverse
(
'article_
list'
)
return
reverse
(
'article_
detail'
,
kwargs
=
{
'pk'
:
self
.
object
.
article
.
pk
}
)
class
ArticleUpdateView
(
FormView
):
class
ArticleUpdateView
(
FormView
):
...
@@ -133,13 +137,15 @@ class AuthorView(View):
...
@@ -133,13 +137,15 @@ class AuthorView(View):
return
redirect
(
'author_list'
)
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
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
CommentForm
()
form
=
CommentForm
()
return
render
(
request
,
"articles/detail.html"
,
context
=
{
context
=
{
'form'
:
form
,
'article'
:
Article
.
objects
.
get
(
pk
=
kwargs
.
get
(
'pk'
))}
'article'
:
get_object_or_404
(
Article
,
pk
=
kwargs
.
get
(
"pk"
)),
return
render
(
request
=
request
,
template_name
=
self
.
template_name
,
context
=
context
)
'form'
:
form
})
class
CommentView
(
View
):
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