Django基础,Day5
来源:互联网 发布:dota2 maybe知乎 编辑:程序博客网 时间:2024/05/17 05:12
投票URL
polls/urls.py:
1
2
# ex: /polls/5/vote/
url(r
'^(?P<question_id>[0-9]+)/vote/$'
, views.vote, name
=
'vote'
),
创建投票form表单
polls/templates/polls/detail.html:
1
2
3
4
5
6
7
8
9
10
11
12
<
h1
>{{ question.question_text }}</
h1
>
{% if error_message %}<
p
><
strong
>{{ error_message }}</
strong
></
p
>{% endif %}
<
form
action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% 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 %}
<
input
type="submit" value="Vote"/>
</
form
>
代码解释:
{{ forloop.counter }} : for 循环次数
{% csrf_token %} : 解决POST请求跨域问题 Cross Site Request Forgeries
{% if %} {% endif %}:判断
{% for %} {% endfor %}:循环
投票Views 逻辑处理
投票 views vote in polls/views.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from
django.http
import
HttpResponse, HttpResponseRedirect
from
django.urls
import
reverse
def
vote(request, question_id):
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,
'polls/detail.html'
, {
'question'
: question,
'error_message'
:
"You didn't select a choice."
,
})
else
:
selected_choice.votes
+
=
1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return
HttpResponseRedirect(reverse(
'polls:results'
, args
=
(question.
id
,)))
代码解释:
request.POST,request.GET: 分别可获得POST或GET方法的参数值,如上 request.POST['choice'] 可取得POST请求中,name值为choice的Value值。若POST参数中没有choice,则会产生KeyError。
HttpResponseRedirect:重定向跳转,提交成功后进行自动重定向跳转是web 开发的一个良好实践。防止数据提交两次。
投票结果页 Views 逻辑处理
views results in polls/views.py:
1
2
3
4
5
from
django.shortcuts
import
get_object_or_404, render
def
results(request, question_id):
question
=
get_object_or_404(Question, pk
=
question_id)
return
render(request,
'polls/results.html'
, {
'question'
: question})
投票结果显示 template
polls/templates/polls/results.html:
1
2
3
4
5
6
7
8
9
<
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 'polls:detail' question.id %}">Vote again?</
a
>
实现结果
1、访问detail.html:http://localhost:8000/polls/1/
2、选择后,点击vote投票,跳转到结果页 results.html
阅读全文
0 0
- Django基础,Day5
- Java基础补习Day5
- 传智播客-Java基础加强-day5
- day5
- day5
- day5
- Day5
- DAY5
- Day5
- DAY5:
- day5
- DAY5
- day5
- Day5
- day5
- day5
- Day5
- day5
- JAVA随笔记(1)BeanUtils.copyProperties用法
- IOS11使用IQKeyboardManager导致tableview位置变形
- 004-dom练习题
- STL容器里存放对象还是指针(转载)
- Warning [gazebo.cc:215] Waited 1seconds for namespaces.
- Django基础,Day5
- 利用Swoole实现PHP+websocket 聊天室
- 关于 cx_Freeze 的中文 API
- 数据导入HBase常用方法
- 微博回应争议条款 今日头条再次“中枪”
- 如何打造95后喜爱的产品?来看看行业大咖的分享
- 前方高能 摩拜送礼送香港迪士尼门票玩转万圣节
- 学习AI哪家强?NVIDIA DLI课程表来了
- 无人驾驶时代,该重新定义道路了!