Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN开源Django项目
Django_Kaoshi_Demo
提交
6821b089
Django_Kaoshi_Demo
项目概览
CSDN开源Django项目
/
Django_Kaoshi_Demo
通知
35
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
Django_Kaoshi_Demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6821b089
编写于
12月 28, 2021
作者:
huidaoli
🚶
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
第4课代码
第4课代码
上级
ee9bf0db
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
62 addition
and
23 deletion
+62
-23
mysite/db.sqlite3
mysite/db.sqlite3
+0
-0
mysite/kaoshi/__pycache__/urls.cpython-39.pyc
mysite/kaoshi/__pycache__/urls.cpython-39.pyc
+0
-0
mysite/kaoshi/__pycache__/views.cpython-39.pyc
mysite/kaoshi/__pycache__/views.cpython-39.pyc
+0
-0
mysite/kaoshi/templates/kaoshi/detail.html
mysite/kaoshi/templates/kaoshi/detail.html
+12
-6
mysite/kaoshi/templates/kaoshi/results.html
mysite/kaoshi/templates/kaoshi/results.html
+9
-0
mysite/kaoshi/urls.py
mysite/kaoshi/urls.py
+3
-3
mysite/kaoshi/views.py
mysite/kaoshi/views.py
+38
-14
未找到文件。
mysite/db.sqlite3
浏览文件 @
6821b089
无法预览此类型文件
mysite/kaoshi/__pycache__/urls.cpython-39.pyc
浏览文件 @
6821b089
无法预览此类型文件
mysite/kaoshi/__pycache__/views.cpython-39.pyc
浏览文件 @
6821b089
无法预览此类型文件
mysite/kaoshi/templates/kaoshi/detail.html
浏览文件 @
6821b089
<h1>
{{ question.question_text }}
</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>
{{ choice.choice_text }}
</li>
{% endfor %}
</ul>
\ No newline at end of file
<form
action=
"{% url 'kaoshi:vote' question.id %}"
method=
"post"
>
{% csrf_token %}
<fieldset>
<legend><h1>
{{ question.question_text }}
</h1></legend>
{% if error_message %}
<p><strong>
{{ error_message }}
</strong></p>
{% endif %}
{% for choice in question.choice_set.all %}
<input
type=
"radio"
name=
"choice"
id=
"choice{{ forloop.counter }}"
value=
"{{ choice.id }}"
>
<label
for=
"choice{{ forloop.counter }}"
>
{{ choice.choice_text }}
</label><br>
{% endfor %}
</fieldset>
<input
type=
"submit"
value=
"Vote"
>
</form>
\ No newline at end of file
mysite/kaoshi/templates/kaoshi/results.html
0 → 100644
浏览文件 @
6821b089
<h1>
{{ question.question_text }}
</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>
{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
</li>
{% endfor %}
</ul>
<a
href=
"{% url 'kaoshi:detail' question.id %}"
>
Vote again?
</a>
\ No newline at end of file
mysite/kaoshi/urls.py
浏览文件 @
6821b089
...
...
@@ -4,8 +4,8 @@ from . import views
app_name
=
'kaoshi'
urlpatterns
=
[
path
(
''
,
views
.
index
,
name
=
'index'
),
path
(
'<int:
question_id>/'
,
views
.
detail
,
name
=
'detail'
),
path
(
'<int:
question_id>/results/'
,
views
.
results
,
name
=
'results'
),
path
(
''
,
views
.
IndexView
.
as_view
()
,
name
=
'index'
),
path
(
'<int:
pk>/'
,
views
.
DetailView
.
as_view
()
,
name
=
'detail'
),
path
(
'<int:
pk>/results/'
,
views
.
ResultsView
.
as_view
()
,
name
=
'results'
),
path
(
'<int:question_id>/vote/'
,
views
.
vote
,
name
=
'vote'
),
]
\ No newline at end of file
mysite/kaoshi/views.py
浏览文件 @
6821b089
from
django.http
import
HttpResponse
from
django.http
import
Http404
from
django.http
import
HttpResponse
,
HttpResponseRedirect
from
django.shortcuts
import
get_object_or_404
,
render
from
.models
import
Question
from
django.urls
import
reverse
from
django.views
import
generic
from
.models
import
Choice
,
Question
from
django.utils
import
timezone
def
index
(
request
):
latest_question_list
=
Question
.
objects
.
order_by
(
'-pub_date'
)[:
5
]
context
=
{
'latest_question_list'
:
latest_question_list
}
return
render
(
request
,
'kaoshi/index.html'
,
context
)
class
IndexView
(
generic
.
ListView
):
template_name
=
'kaoshi/index.html'
context_object_name
=
'latest_question_list'
def
detail
(
request
,
question_id
):
question
=
get_object_or_404
(
Question
,
pk
=
question_id
)
return
render
(
request
,
'kaoshi/detail.html'
,
{
'question'
:
question
})
def
get_queryset
(
self
):
"""
Return the last five published questions (not including those set to be
published in the future).
"""
return
Question
.
objects
.
filter
(
pub_date__lte
=
timezone
.
now
()
).
order_by
(
'-pub_date'
)[:
5
]
class
DetailView
(
generic
.
DetailView
):
model
=
Question
template_name
=
'kaoshi/detail.html'
def
results
(
request
,
question_id
):
response
=
"You're looking at the results of question %s."
return
HttpResponse
(
response
%
question_id
)
class
ResultsView
(
generic
.
DetailView
):
model
=
Question
template_name
=
'kaoshi/results.html'
def
vote
(
request
,
question_id
):
return
HttpResponse
(
"You're voting on question %s."
%
question_id
)
\ No newline at end of file
question
=
get_object_or_404
(
Question
,
pk
=
question_id
)
try
:
selected_choice
=
question
.
choice_set
.
get
(
pk
=
request
.
POST
[
'choice'
])
except
(
KeyError
,
Choice
.
DoesNotExist
):
# Redisplay the question voting form.
return
render
(
request
,
'kaoshi/detail.html'
,
{
'question'
:
question
,
'error_message'
:
"You didn't select a choice."
,
})
else
:
selected_choice
.
votes
+=
1
selected_choice
.
save
()
return
HttpResponseRedirect
(
reverse
(
'kaoshi:results'
,
args
=
(
question
.
id
,)))
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录