Django中文官方版08-创建简单表单
来源:互联网 发布:通往天堂的钥匙淘宝网 编辑:程序博客网 时间:2024/05/19 16:05
注:表单传递方式类似jsp+servlet
1.更新polls/templates/polls/detail.html内容
输入:
<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>
注:{{}}代表参数值,{%%}代表语意标签2.打开polls/urls.py文件
输入以下内容:
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
注:使用vote方法来处理表单提交的内容3.打开polls/views.py文件
修改vote方法的内容:
from django.shortcuts import get_object_or_404, renderfrom django.http import HttpResponseRedirect, HttpResponsefrom django.urls import reversefrom .models import Choice, Question# ...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['choice']代表通过post方式传递接收道德参数choice
4.处理表单结束后跳转处理
打开polls/views.py
修改results方法内容:
from django.shortcuts import get_object_or_404, renderdef results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question})
5.添加results.html模板文件在template/polls下面添加results.html文件
输入以下内容:
<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>
6.启动runserver,测试表单提交结果
python manage.py runserver
输入127.0.0.1:8080
/polls/1/
原文摘自官方地址https://docs.djangoproject.com/en/1.11/intro/tutorial04/,本文只做精简化翻译,详细内容可去指定地址阅读
阅读全文
0 0
- Django中文官方版08-创建简单表单
- Django中文官方版01-创建项目
- Django中文官方版02-创建app
- Django中文官方版03-配置数据库
- Django中文官方版06-修改视图
- Django中文官方版04-常用数据库API
- Django中文官方版05-配置后台管理
- Django中文官方版07-添加视图模板
- Django中文官方版09-使用通用视图
- Django官方【实例】-简单的全方位例子
- Django从models创建表单(forms)
- django 1.8 官方文档翻译: 5-1-1 使用表单
- django 1.8 官方文档翻译:5-1-2 表单API
- django下简单表单验证实现上
- django表单处理(一)--最简单的表单
- Django REST Framework v.3官方中文教程 索引
- [Django与表单]写一个简单的Django form表单处理
- Django 表单
- 文章标题
- 关于接口中语言不同,用的方法可以试着换下
- JSON校验工具
- 【恶意代码分析】关键函数备忘录
- c++中内置的 string 类
- Django中文官方版08-创建简单表单
- 如何让外网访问本地服务器
- 计算药量项目中的TabControl控件使用
- BoxLayout布局管理器
- Jquery ajaxFileUpload的一些心得
- Android Binder
- Linux 字符设备驱动
- ios 中的小技巧
- EditText如何在使用数字键盘的情况下输入多个小数点