Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-11_http_framework
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
Давид Ли
ap-11_http_framework
Commits
d1ebeea8
Commit
d1ebeea8
authored
Jul 12, 2023
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
webinar 24
parent
8592d5b4
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
77 additions
and
37 deletions
+77
-37
styles.css
banana/css/styles.css
+0
-3
settings.py
core/settings.py
+4
-0
forms.py
web/forms.py
+16
-1
article.html
web/templates/article.html
+0
-18
article_create.html
web/templates/article_create.html
+3
-2
article_detail.html
web/templates/article_detail.html
+30
-0
form.html
web/templates/partial/form.html
+16
-0
views.py
web/views.py
+8
-13
No files found.
banana/css/styles.css
View file @
d1ebeea8
...
@@ -11,15 +11,12 @@
...
@@ -11,15 +11,12 @@
body
{
body
{
font-family
:
Arial
,
sans-serif
;
font-family
:
Arial
,
sans-serif
;
font-size
:
16px
;
font-size
:
16px
;
color
:
#444444
;
background-color
:
pink
;
}
}
.container
{
.container
{
margin
:
0
auto
;
margin
:
0
auto
;
width
:
100%
;
width
:
100%
;
max-width
:
900px
;
max-width
:
900px
;
text-align
:
center
;
}
}
.field-error
{
.field-error
{
...
...
core/settings.py
View file @
d1ebeea8
...
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
...
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'django.contrib.staticfiles'
,
'django_extensions'
,
'web'
'web'
]
]
...
@@ -134,3 +135,6 @@ STATICFILES_DIRS = [
...
@@ -134,3 +135,6 @@ STATICFILES_DIRS = [
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD
=
'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD
=
'django.db.models.BigAutoField'
SHELL_PLUS
=
"bpython"
web/forms.py
View file @
d1ebeea8
from
django
import
forms
from
django
import
forms
from
django.forms
import
widgets
from
django.forms
import
widgets
from
web.models
import
Article
class
ArticleForm
(
forms
.
Form
):
class
ArticleForm
(
forms
.
Form
):
title
=
forms
.
CharField
(
max_length
=
200
,
required
=
True
,
label
=
'Title'
)
title
=
forms
.
CharField
(
max_length
=
200
,
required
=
True
,
label
=
'Title'
)
author
=
forms
.
CharField
(
max_length
=
40
,
required
=
True
,
label
=
'Author'
)
author
=
forms
.
CharField
(
max_length
=
40
,
required
=
True
,
label
=
'Author'
)
status
=
forms
.
CharField
(
required
=
True
,
label
=
'Status'
)
status
=
forms
.
CharField
(
required
=
True
,
label
=
'Status'
)
text
=
forms
.
CharField
(
max_length
=
3000
,
required
=
True
,
label
=
'Text'
,
widget
=
widgets
.
Textarea
)
text
=
forms
.
CharField
(
max_length
=
3000
,
required
=
True
,
label
=
'Text'
,
widget
=
widgets
.
Textarea
)
\ No newline at end of file
class
ArticleModelForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Article
fields
=
(
'title'
,
'author'
,
'status'
,
'text'
,
'tags'
)
widgets
=
{
'title'
:
forms
.
TextInput
(
attrs
=
{
'class'
:
'form-control mb-3'
}),
'author'
:
forms
.
TextInput
(
attrs
=
{
'class'
:
'form-control mb-3'
}),
'status'
:
forms
.
Select
(
attrs
=
{
'class'
:
'form-select mb-3'
}),
'text'
:
forms
.
Textarea
(
attrs
=
{
'class'
:
'form-control mb-3'
}),
'tags'
:
forms
.
CheckboxSelectMultiple
(
attrs
=
{
'class'
:
'mb-3'
})
}
web/templates/article.html
deleted
100644 → 0
View file @
8592d5b4
{% extends 'base.html' %}
{% block title %}
<h1>
Article checkout
</h1>
{% endblock %}
{% block content %}
<h2>
{{ article.title }}
</h2>
<p>
{{ article.text }}
</p>
<h5>
Status: {{ article.status }}
</h5>
<h5>
By: {{ article.author }}
</h5>
<h4>
Created: {{ article.created_at }}
</h4>
<h4>
Updated: {{ article.updated_at }}
</h4>
<a
href=
"{% url 'article_update' article.pk %}"
>
Edit
</a>
<a
href=
"{% url 'delete_article' article.pk %}"
>
Delete
</a>
{% endblock %}
web/templates/article_create.html
View file @
d1ebeea8
...
@@ -3,12 +3,13 @@
...
@@ -3,12 +3,13 @@
{% load static %}
{% load static %}
{% block title %}
{% block title %}
<h1>
Create Article
</h1>
<h1
class=
"text-center my-4"
>
Create Article
</h1>
{% endblock %}
{% endblock %}
{% block content %}
{% block content %}
<form
action=
"/articles/add/"
method=
"post"
>
<form
action=
"/articles/add/"
method=
"post"
>
{% include 'partial/article_form.html' with button_text='Create' %}
{% csrf_token %}
{% include 'partial/form.html' with button_text='Create' %}
</form>
</form>
{% endblock %}
{% endblock %}
web/templates/article_detail.html
0 → 100644
View file @
d1ebeea8
{% extends 'base.html' %}
{% block title %}
<h2
class=
"my-4"
>
Article checkout
</h2>
<hr>
{% endblock %}
{% block content %}
<div><b>
Title:
</b>
{{ article.title }}
</div>
<b>
Tags:
</b>
<ol>
{% for tag in article.tags.all %}
<li>
{{ tag.name }}
</li>
{% endfor %}
</ol>
<div><b>
Text:
</b>
{{ article.text }}
</div>
<div><b>
Status:
</b>
{{ article.status }}
</div>
<div><b>
By:
</b>
{{ article.author }}
</div>
<div><b>
Created:
</b>
{{ article.created_at }}
</div>
<div><b>
Updated:
</b>
{{ article.updated_at }}
</div>
<hr>
<a
href=
"{% url 'article_update' article.pk %}"
class=
"btn btn-primary mt-4 me-3"
>
Edit
</a>
<a
href=
"{% url 'delete_article' article.pk %}"
class=
"btn btn-danger mt-4"
>
Delete
</a>
{% endblock %}
web/templates/partial/form.html
0 → 100644
View file @
d1ebeea8
{% for error in form.non_field_errors %}
<div
class=
"alert alert-danger"
>
{{ error }}
</div>
{% endfor %}
{% for field in form.visible_fields %}
{% if field.errors %}
{% for error in field.errors %}
<div
class=
"alert alert-danger"
>
{{ error }}
</div>
{% endfor %}
{% endif %}
{{ field.label }}
{{ field }}
{% endfor %}
<button
type=
"submit"
class=
"btn btn-success"
>
{{ button_text }}
</button>
web/views.py
View file @
d1ebeea8
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.shortcuts
import
render
,
redirect
,
get_object_or_404
from
django.urls
import
reverse
from
django.urls
import
reverse
from
django.views.generic
import
View
,
TemplateView
,
RedirectView
from
django.views.generic
import
View
,
TemplateView
,
RedirectView
from
web.forms
import
ArticleForm
from
web.forms
import
ArticleForm
,
ArticleModelForm
from
web.models
import
Article
,
StatusChoices
from
web.models
import
Article
,
StatusChoices
def
index_view
(
request
):
def
index_view
(
request
):
return
render
(
return
render
(
request
,
'index.html'
,
context
=
{
request
,
'index.html'
,
context
=
{
'articles'
:
Article
.
objects
.
order_by
(
'
title
'
),
'articles'
:
Article
.
objects
.
order_by
(
'
-created_at
'
),
'reverse'
:
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
1
})
'reverse'
:
reverse
(
'articles-detail'
,
kwargs
=
{
'id'
:
1
})
}
}
)
)
...
@@ -21,7 +21,7 @@ class MainPageRedirectView(RedirectView):
...
@@ -21,7 +21,7 @@ class MainPageRedirectView(RedirectView):
# Class-based views
# Class-based views
class
ArticleCreateView
(
View
):
class
ArticleCreateView
(
View
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
form
=
ArticleForm
()
form
=
Article
Model
Form
()
return
render
(
return
render
(
self
.
request
,
self
.
request
,
...
@@ -35,15 +35,10 @@ class ArticleCreateView(View):
...
@@ -35,15 +35,10 @@ class ArticleCreateView(View):
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
errors
=
{}
errors
=
{}
data
=
request
.
POST
data
=
request
.
POST
form
=
ArticleForm
(
data
=
data
)
form
=
Article
Model
Form
(
data
=
data
)
if
form
.
is_valid
():
if
form
.
is_valid
():
article
=
Article
.
objects
.
create
(
article
=
form
.
save
()
title
=
form
.
cleaned_data
[
'title'
],
return
redirect
(
'articles-detail'
,
id
=
article
.
id
)
status
=
form
.
cleaned_data
[
'status'
],
text
=
form
.
cleaned_data
[
'text'
],
author
=
form
.
cleaned_data
[
'author'
],
)
return
redirect
(
redirect
(
'articles-detail'
,
id
=
article
.
id
))
else
:
else
:
return
render
(
self
.
request
,
'article_create.html'
,
context
=
{
'form'
:
form
,
'status_choices'
:
StatusChoices
.
choices
})
return
render
(
self
.
request
,
'article_create.html'
,
context
=
{
'form'
:
form
,
'status_choices'
:
StatusChoices
.
choices
})
...
@@ -51,11 +46,11 @@ class ArticleCreateView(View):
...
@@ -51,11 +46,11 @@ class ArticleCreateView(View):
# /articles/{id}
# /articles/{id}
def
article_detail_view
(
request
,
id
:
int
):
def
article_detail_view
(
request
,
id
:
int
):
article
=
get_object_or_404
(
Article
,
id
=
id
)
article
=
get_object_or_404
(
Article
,
id
=
id
)
return
render
(
request
,
'article.html'
,
context
=
{
'article'
:
article
})
return
render
(
request
,
'article
_detail
.html'
,
context
=
{
'article'
:
article
})
class
ArticleDetailView
(
TemplateView
):
class
ArticleDetailView
(
TemplateView
):
template_name
=
'article.html'
template_name
=
'article
_detail
.html'
def
get_context_data
(
self
,
**
kwargs
):
def
get_context_data
(
self
,
**
kwargs
):
article
=
get_object_or_404
(
Article
,
id
=
kwargs
.
get
(
'id'
))
article
=
get_object_or_404
(
Article
,
id
=
kwargs
.
get
(
'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