Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-11_django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
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-11_django
Commits
b2b362a5
Commit
b2b362a5
authored
Jul 19, 2023
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
web 24
parent
76d5ea91
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
125 additions
and
119 deletions
+125
-119
urls.py
core/urls.py
+16
-7
forms.py
web/forms.py
+3
-2
article_create.html
web/templates/article/article_create.html
+0
-0
article_detail.html
web/templates/article/article_detail.html
+19
-3
delete.html
web/templates/article/delete.html
+0
-0
index.html
web/templates/article/index.html
+0
-0
update.html
web/templates/article/update.html
+1
-1
base.html
web/templates/base.html
+1
-1
create_update.html
web/templates/comment/create_update.html
+15
-0
article_form.html
web/templates/partial/article_form.html
+0
-76
form.html
web/templates/partial/form.html
+5
-0
__init__.py
web/views/__init__.py
+17
-0
article.py
web/views/article.py
+8
-29
comment.py
web/views/comment.py
+40
-0
No files found.
core/urls.py
View file @
b2b362a5
...
...
@@ -22,17 +22,26 @@ from web import views
urlpatterns
=
[
path
(
'admin/'
,
admin
.
site
.
urls
),
path
(
''
,
views
.
MainPageRedirectView
.
as_view
()),
path
(
'articles/'
,
views
.
ArticleIndexView
.
as_view
(),
name
=
'main_page'
),
path
(
'articles/add/'
,
views
.
ArticleCreateView
.
as_view
(),
name
=
'articles-add'
),
# /articles/1/ OR /articles/?id=1
# article_details_view(request, id)
path
(
'articles/<int:id>/'
,
views
.
ArticleDetailView
.
as_view
(),
name
=
'articles-detail'
),
path
(
'articles/<int:id>/edit'
,
views
.
ArticleUpdateView
.
as_view
(),
name
=
'article_update'
),
path
(
'articles/<int:id>/delete'
,
views
.
ArticleDeleteView
.
as_view
(),
name
=
'delete_article'
),
path
(
'
articles/<int:article_id>/comments
/'
,
views
.
Article
CommentCreateView
.
as_view
(),
name
=
'
article_
comment_create'
'
comments/add
/'
,
views
.
CommentCreateView
.
as_view
(),
name
=
'comment_create'
),
path
(
'articles/<int:id>/delete'
,
views
.
ArticleDeleteView
.
as_view
(),
name
=
'delete_article'
)
path
(
'comments/<int:id>/edit'
,
views
.
CommentUpdateView
.
as_view
(),
name
=
'comment_update'
),
path
(
'comments/<int:id>/delete'
,
views
.
CommentDeleteView
.
as_view
(),
name
=
'comment_delete'
)
]
+
static
(
settings
.
MEDIA_URL
,
document_root
=
settings
.
MEDIA_ROOT
)
web/forms.py
View file @
b2b362a5
...
...
@@ -32,13 +32,14 @@ class ArticleModelForm(forms.ModelForm):
return
cleaned_data
class
Article
CommentModelForm
(
forms
.
ModelForm
):
class
CommentModelForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Comment
fields
=
[
'author'
,
'text'
]
fields
=
[
'author'
,
'text'
,
'article'
]
widgets
=
{
'text'
:
forms
.
TextInput
(
attrs
=
{
'class'
:
'form-control mb-3'
}),
'author'
:
forms
.
TextInput
(
attrs
=
{
'class'
:
'form-control mb-3'
}),
'article'
:
forms
.
HiddenInput
()
}
...
...
web/templates/article_create.html
→
web/templates/article
/article
_create.html
View file @
b2b362a5
File moved
web/templates/article_detail.html
→
web/templates/article
/article
_detail.html
View file @
b2b362a5
...
...
@@ -26,21 +26,36 @@
<hr>
<a
href=
"{% url 'article_update' article.pk %}"
class=
"btn btn-primary mt-4 me-3"
>
Edit
</a>
<form
action=
"{% url 'delete_article' article.pk %}"
method=
"POST"
onsubmit=
"confirm('Are you sure?')"
>
<form
action=
"{% url 'delete_article' article.pk %}"
method=
"POST"
onsubmit=
"
return
confirm('Are you sure?')"
>
{% csrf_token %}
<button
class=
"btn btn-danger mt-4"
>
Delete
</button>
</form>
<h3
class=
"mt-4"
>
Comments:
</h3>
<div
class=
"comments-list"
>
<form
action=
"{% url 'article_comment_create' article_id=article.id %}"
method=
"POST"
>
<form
action=
"{% url 'comment_create' %}"
method=
"POST"
>
{% csrf_token %}
{% include 'partial/form.html' with button_text='add' %}
<input
type=
"hidden"
name=
"article"
value=
"{{ article.id }}"
>
</form>
{% for comment in comments %}
<div
class=
"card my-3"
>
<div
class=
"card-header"
>
<div
class=
"card-header
d-flex justify-content-between
"
>
{{ comment.author }}
<div
class=
"d-flex justify-content-between"
>
<a
href=
"{% url 'comment_update' id=comment.id %}"
class=
"me-5"
>
<i
class=
"fa fa-regular fa-pen-to-square"
></i>
</a>
<form
action=
"{% url 'comment_delete' id=comment.id %}"
method=
"POST"
onsubmit=
"return confirm('Are you sure?')"
>
{% csrf_token %}
<button
type=
"submit"
class=
"border-0 text-danger"
><i
class=
"fa-solid fa-trash"
></i></button>
</form>
</div>
</div>
<div
class=
"card-body"
>
<blockquote
class=
"blockquote mb-0"
>
...
...
@@ -52,5 +67,6 @@
{% empty %}
<p>
No comments yet.
</p>
{% endfor %}
</div>
{% endblock %}
web/templates/delete.html
→
web/templates/
article/
delete.html
View file @
b2b362a5
File moved
web/templates/index.html
→
web/templates/
article/
index.html
View file @
b2b362a5
File moved
web/templates/update.html
→
web/templates/
article/
update.html
View file @
b2b362a5
...
...
@@ -6,7 +6,7 @@
{% block content %}
<form
action=
"{% url 'article_update' article.pk %}"
method=
"post"
>
{% include 'partial/
article_
form.html' with button_text='Update' abc='123' %}
{% include 'partial/form.html' with button_text='Update' abc='123' %}
</form>
{% endblock %}
web/templates/base.html
View file @
b2b362a5
...
...
@@ -6,7 +6,7 @@
<link
rel=
"stylesheet"
href=
"https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/css/bootstrap.min.css"
integrity=
"sha512-Ez0cGzNzHR1tYAv56860NLspgUGuQw16GiOOp/I2LuTmpSK9xDXlgJz3XN4cnpXWDmkNBKXR/VDMTCnAaEooxA=="
crossorigin=
"anonymous"
referrerpolicy=
"no-referrer"
/>
<link
rel=
"stylesheet"
href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity=
"sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin=
"anonymous"
referrerpolicy=
"no-referrer"
/>
<link
rel=
"stylesheet"
href=
"{% static 'css/styles.css' %}"
>
<title>
Title
</title>
</head>
...
...
web/templates/comment/create_update.html
0 → 100644
View file @
b2b362a5
{% extends 'base.html' %}
{% load static %}
{% block title %}
<h1
class=
"text-center my-4"
>
{{ title }}
</h1>
{% endblock %}
{% block content %}
<form
action=
""
method=
"post"
>
{% csrf_token %}
{% include 'partial/form.html' %}
</form>
{% endblock %}
web/templates/partial/article_form.html
deleted
100644 → 0
View file @
76d5ea91
{% csrf_token %}
<!-- <div class="mb-3">
<label for="titleId" class="form-label">Title</label>
{% if errors.title %}
<p class="form-error">{{ errors.title }}</p>
<input type="text" name="title" class="form-control field-error" id="titleId" aria-describedby="title" value="{{ article.title }}">
{%else%}
<input type="text" name="title" class="form-control" id="titleId" aria-describedby="title" value="{{ article.title }}">
{%endif%}
</div>
<div class="mb-3">
<label for="statusId" class="form-label">Status</label>
<select name="status" id="statusId" class="form-select">
{% for choice in status_choices %}
{% if article.status == choice.0 %}
<option value="{{ choice.0 }}" selected>{{ choice.1 }}</option>
{% else %}
<option value="{{ choice.0 }}">{{ choice.1 }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="mb-3">
<label for="textId" class="form-label">Text</label>
{% if errors.text %}
<p class="form-error">{{ errors.text }}</p>
<textarea name="text" class="form-control field-error" id="textId" cols="30" rows="10">{{article.text}}</textarea>
{% else %}
<textarea name="text" class="form-control" id="textId" cols="30" rows="10">{{article.text}}</textarea>
{%endif%}
</div>
<div class="mb-3">
<label for="authorId" class="form-label">Author</label>
{% if errors.author %}
<p class="form-error">{{ errors.author }}</p>
<input type="text" name="author" class="form-control field-error" id="authorId" aria-describedby="author" value="{{article.author}}">
{%else%}
<input type="text" name="author" class="form-control" id="authorId" aria-describedby="author" value="{{article.author}}">
{%endif%}
</div> -->
{% for error in form.non_field_errors %}
<p
class=
"form-error"
>
{{error}}
</p>
{%endfor%}
<p><label
for=
"{{form.title.id_for_label}}"
>
{{form.title.label}}
</label></p>
{% for error in form.title.errors %}
<p
class=
"form-error"
>
{{ error }}
</p>
{% endfor %}
<p>
{{ form.title }}
</p>
<p><label
for=
"{{form.author.id_for_label}}"
>
{{form.author.label}}
</label></p>
{% for error in form.author.errors %}
<p
class=
"form-error"
>
{{ error }}
</p>
{% endfor %}
<p>
{{ form.author }}
</p>
<p><label
for=
"{{form.status.id_for_label}}"
>
{{form.status.label}}
</label></p>
{% for error in form.status.errors %}
<p
class=
"form-error"
>
{{ error }}
</p>
{% endfor %}
<p>
{{ form.status }}
</p>
<p><label
for=
"{{form.text.id_for_label}}"
>
{{form.text.label}}
</label></p>
{% for error in form.text.errors %}
<p
class=
"form-error"
>
{{ error }}
</p>
{% endfor %}
<p>
{{ form.text }}
</p>
<button
type=
"submit"
class=
"btn btn-primary"
>
{{ button_text }}
</button>
\ No newline at end of file
web/templates/partial/form.html
View file @
b2b362a5
...
...
@@ -13,4 +13,9 @@
{{ field }}
{% endfor %}
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
<button
type=
"submit"
class=
"btn btn-success"
>
{{ button_text }}
</button>
web/views/__init__.py
0 → 100644
View file @
b2b362a5
from
django.views.generic
import
RedirectView
from
web.views.article
import
(
ArticleDetailView
,
ArticleIndexView
,
ArticleUpdateView
,
ArticleCreateView
,
ArticleDeleteView
)
from
web.views.comment
import
(
CommentCreateView
,
CommentUpdateView
,
CommentDeleteView
)
class
MainPageRedirectView
(
RedirectView
):
pattern_name
=
'main_page'
web/views.py
→
web/views
/article
.py
View file @
b2b362a5
from
urllib.parse
import
urlencode
from
django.shortcuts
import
get_object_or_404
from
django.urls
import
reverse
,
reverse_lazy
from
django.db.models
import
Q
from
django.views.generic
import
(
...
...
@@ -11,12 +10,12 @@ from django.views.generic import (
UpdateView
,
DeleteView
)
from
web.forms
import
ArticleModelForm
,
SearchForm
,
Article
CommentModelForm
from
web.models
import
Article
,
Comment
from
web.forms
import
ArticleModelForm
,
SearchForm
,
CommentModelForm
from
web.models
import
Article
class
ArticleIndexView
(
ListView
):
template_name
=
'index.html'
template_name
=
'
article/
index.html'
context_object_name
=
'articles'
model
=
Article
ordering
=
[
'-created_at'
]
...
...
@@ -52,12 +51,8 @@ class ArticleIndexView(ListView):
return
self
.
form
.
cleaned_data
.
get
(
'search'
)
class
MainPageRedirectView
(
RedirectView
):
pattern_name
=
'main_page'
class
ArticleCreateView
(
CreateView
):
template_name
=
'article_create.html'
template_name
=
'article
/article
_create.html'
model
=
Article
form_class
=
ArticleModelForm
...
...
@@ -65,28 +60,12 @@ class ArticleCreateView(CreateView):
return
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
self
.
object
.
id
})
class
ArticleCommentCreateView
(
CreateView
):
model
=
Comment
form_class
=
ArticleCommentModelForm
def
dispatch
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
article
=
get_object_or_404
(
Article
,
id
=
self
.
kwargs
.
get
(
'article_id'
))
return
super
()
.
dispatch
(
request
,
*
args
,
**
kwargs
)
def
form_valid
(
self
,
form
):
form
.
instance
.
article
=
self
.
article
return
super
()
.
form_valid
(
form
)
def
get_success_url
(
self
):
return
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
self
.
article
.
id
})
class
ArticleDetailView
(
DetailView
):
template_name
=
'article_detail.html'
template_name
=
'article
/article
_detail.html'
model
=
Article
context_object_name
=
'article'
pk_url_kwarg
=
'id'
extra_context
=
{
'form'
:
Article
CommentModelForm
}
extra_context
=
{
'form'
:
CommentModelForm
}
def
get_context_data
(
self
,
**
kwargs
):
return
super
()
.
get_context_data
(
...
...
@@ -97,7 +76,7 @@ class ArticleDetailView(DetailView):
class
ArticleUpdateView
(
UpdateView
):
model
=
Article
template_name
=
'update.html'
template_name
=
'
article/
update.html'
form_class
=
ArticleModelForm
context_object_name
=
'article'
pk_url_kwarg
=
'id'
...
...
@@ -108,7 +87,7 @@ class ArticleUpdateView(UpdateView):
class
ArticleDeleteView
(
DeleteView
):
model
=
Article
template_name
=
'delete.html'
template_name
=
'
article/
delete.html'
context_object_name
=
'article'
success_url
=
reverse_lazy
(
'main_page'
)
pk_url_kwarg
=
'id'
web/views/comment.py
0 → 100644
View file @
b2b362a5
from
django.urls
import
reverse
from
django.views.generic
import
(
CreateView
,
UpdateView
,
DeleteView
)
from
web.forms
import
CommentModelForm
from
web.models
import
Comment
class
CommentCreateView
(
CreateView
):
model
=
Comment
template_name
=
'comment/create_update.html'
form_class
=
CommentModelForm
def
get_success_url
(
self
):
return
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
self
.
object
.
article
.
id
})
class
CommentUpdateView
(
UpdateView
):
model
=
Comment
template_name
=
'comment/create_update.html'
context_object_name
=
'comment'
pk_url_kwarg
=
'id'
form_class
=
CommentModelForm
extra_context
=
{
'button_text'
:
'Update'
,
'title'
:
'Update comment'
}
def
get_initial
(
self
):
return
self
.
object
.
__dict__
def
get_success_url
(
self
):
return
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
self
.
object
.
article
.
id
})
class
CommentDeleteView
(
DeleteView
):
model
=
Comment
pk_url_kwarg
=
'id'
def
get_success_url
(
self
):
return
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
self
.
object
.
article
.
id
})
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