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
a83f5c88
Commit
a83f5c88
authored
Jan 15, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 47
parent
de1e954d
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
117 additions
and
21 deletions
+117
-21
urls.py
ap_12/core/urls.py
+2
-0
db.sqlite3
ap_12/db.sqlite3
+0
-0
create.html
ap_12/templates/articles/create.html
+1
-14
detail.html
ap_12/templates/articles/detail.html
+10
-0
update.html
ap_12/templates/articles/update.html
+9
-0
article_form.html
ap_12/templates/partial/article_form.html
+28
-0
forms.py
ap_12/webapp/forms.py
+11
-0
views.py
ap_12/webapp/views.py
+56
-7
No files found.
ap_12/core/urls.py
View file @
a83f5c88
...
@@ -23,4 +23,6 @@ urlpatterns = [
...
@@ -23,4 +23,6 @@ urlpatterns = [
path
(
''
,
views
.
index_view
,
name
=
'index'
),
path
(
''
,
views
.
index_view
,
name
=
'index'
),
path
(
'articles/add'
,
views
.
article_create_view
,
name
=
'articles_add'
),
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
.
article_detail_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/db.sqlite3
View file @
a83f5c88
No preview for this file type
ap_12/templates/articles/create.html
View file @
a83f5c88
...
@@ -4,19 +4,6 @@
...
@@ -4,19 +4,6 @@
<h1>
Create Article
</h1>
<h1>
Create Article
</h1>
<form
action=
"{% url 'articles_add' %}"
method=
"post"
>
<form
action=
"{% url 'articles_add' %}"
method=
"post"
>
{% csrf_token %}
{% include 'partial/article_form.html' with btn_text='Create' %}
<label>
Title:
</label><br>
<input
type=
"text"
name=
"title"
>
<br>
<label>
Text:
</label><br>
<textarea
name=
"text"
></textarea><br>
<label>
Author:
</label><br>
<input
type=
"text"
name=
"author"
><br>
<br>
<input
type=
"submit"
value=
"Send"
>
</form>
</form>
{% endblock %}
{% endblock %}
\ No newline at end of file
ap_12/templates/articles/detail.html
View file @
a83f5c88
...
@@ -6,4 +6,14 @@
...
@@ -6,4 +6,14 @@
<h2>
{{ article.title }}
</h2>
<h2>
{{ article.title }}
</h2>
<p>
{{ article.text }}
</p>
<p>
{{ article.text }}
</p>
<p>
By: {{ article.author }}
</p>
<p>
By: {{ article.author }}
</p>
<a
href=
"{% url 'articles_update' id=article.id %}"
class=
"btn btn-primary"
>
Update
</a>
<form
action=
"{% url 'articles_delete' id=article.id %}"
method=
"post"
onsubmit=
"return confirm('Delete?')"
>
{% csrf_token %}
<button
class=
"btn btn-danger"
>
Delete
</button>
</form>
{% endblock %}
{% endblock %}
\ No newline at end of file
ap_12/templates/articles/update.html
0 → 100644
View file @
a83f5c88
{% extends 'base.html' %}
{% block content %}
<h1>
Update Article
</h1>
<form
action=
"{% url 'articles_update' id=article.id %}"
method=
"post"
>
{% include 'partial/article_form.html' %}
</form>
{% endblock %}
\ No newline at end of file
ap_12/templates/partial/article_form.html
0 → 100644
View file @
a83f5c88
{% csrf_token %}
{% for error in form.non_field_errors %}
<p
class=
"text-danger"
>
{{ error }}
</p>
{% endfor %}
<label>
Title:
</label><br>
<input
type=
"text"
name=
"title"
value=
"{{ article.title }}"
>
{% for error in form.title.errors %}
<p
class=
"text-danger"
>
{{ error }}
</p>
{% endfor %}
<br>
<label>
Text:
</label><br>
<textarea
name=
"text"
>
{{ article.text }}
</textarea><br>
{% for error in form.text.errors %}
<p
class=
"text-danger"
>
{{ error }}
</p>
{% endfor %}
<label>
Author:
</label><br>
<input
type=
"text"
name=
"author"
value=
"{{ article.author }}"
><br>
{% for error in form.author.errors %}
<p
class=
"text-danger"
>
{{ error }}
</p>
{% endfor %}
<br>
<input
type=
"submit"
value=
"{{ btn_text|default:'Send' }}"
class=
"btn btn-primary"
>
\ No newline at end of file
ap_12/webapp/forms.py
0 → 100644
View file @
a83f5c88
from
django
import
forms
from
django.forms
import
widgets
class
ArticleForm
(
forms
.
Form
):
title
=
forms
.
CharField
(
max_length
=
200
,
required
=
True
,
label
=
'Title'
)
author
=
forms
.
CharField
(
max_length
=
40
,
required
=
True
,
label
=
'Author'
)
text
=
forms
.
CharField
(
max_length
=
3000
,
required
=
True
,
label
=
'Text'
,
widget
=
widgets
.
Textarea
,
)
ap_12/webapp/views.py
View file @
a83f5c88
from
django.shortcuts
import
render
,
get_object_or_404
,
redirect
from
django.shortcuts
import
render
,
get_object_or_404
,
redirect
from
webapp.forms
import
ArticleForm
from
webapp.models
import
Article
from
webapp.models
import
Article
# CRUD - Create Read Update Delete
def
index_view
(
request
):
def
index_view
(
request
):
context
=
{
'articles'
:
Article
.
objects
.
all
()}
context
=
{
'articles'
:
Article
.
objects
.
all
()}
...
@@ -16,14 +20,59 @@ def article_detail_view(request, id):
...
@@ -16,14 +20,59 @@ def article_detail_view(request, id):
def
article_create_view
(
request
):
def
article_create_view
(
request
):
if
request
.
method
==
'GET'
:
if
request
.
method
==
'GET'
:
return
render
(
request
,
'articles/create.html'
)
form
=
ArticleForm
()
return
render
(
request
,
'articles/create.html'
,
context
=
{
'form'
:
form
})
elif
request
.
method
==
'POST'
:
elif
request
.
method
==
'POST'
:
print
(
request
.
POST
)
print
(
request
.
POST
)
form
=
ArticleForm
(
data
=
request
.
POST
)
if
form
.
is_valid
():
article
=
Article
.
objects
.
create
(
article
=
Article
.
objects
.
create
(
title
=
request
.
POST
.
get
(
'title'
),
title
=
request
.
POST
.
get
(
'title'
),
text
=
request
.
POST
.
get
(
'text'
),
text
=
request
.
POST
.
get
(
'text'
),
author
=
request
.
POST
.
get
(
'author'
),
author
=
request
.
POST
.
get
(
'author'
),
)
)
return
redirect
(
'articles_detail'
,
id
=
article
.
id
)
return
redirect
(
'articles_detail'
,
id
=
article
.
id
)
else
:
return
render
(
request
,
'articles/create.html'
,
context
=
{
'form'
:
form
},
)
def
article_update_view
(
request
,
id
):
article
=
get_object_or_404
(
Article
,
id
=
id
)
if
request
.
method
==
'GET'
:
form
=
ArticleForm
(
initial
=
{
'title'
:
article
.
title
,
'text'
:
article
.
text
,
'author'
:
article
.
author
,
})
return
render
(
request
,
'articles/update.html'
,
context
=
{
'article'
:
article
,
'form'
:
form
},
)
elif
request
.
method
==
'POST'
:
form
=
ArticleForm
(
data
=
request
.
POST
)
if
form
.
is_valid
():
article
.
title
=
form
.
cleaned_data
.
get
(
'title'
)
article
.
author
=
form
.
cleaned_data
.
get
(
'author'
)
article
.
text
=
form
.
cleaned_data
.
get
(
'text'
)
article
.
save
()
return
redirect
(
'index'
)
else
:
return
render
(
request
,
'articles/update.html'
,
context
=
{
'form'
:
form
,
'article'
:
article
})
def
article_delete_view
(
request
,
id
):
get_object_or_404
(
Article
,
id
=
id
)
.
delete
()
return
redirect
(
'index'
)
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