接下来是html模板的使用,本人也只对html略知一点点,说的不好的地方请见谅。
我乘urls.py为url捕获器,在这里有些小知识点
![](https://img.haomeiwen.com/i4716162/25dcb10eb68543fd.png)
path(route, view, kwargs=None, name=None)函数
1.第一个参数也就是地址指向,不需要"/"开头,但是一定要"/"结尾。<>这个是url捕获值,更多用法查看官方文档
2.view也就是指向的回调函数
3.kwargs传递额外参数
![](https://img.haomeiwen.com/i4716162/2210bfb676faad3a.png)
4.name用于减少多个应用之间的命名冲突
了解一些url捕获器,接下来编写views.py来添加更多的回调和html模板。
模板先需要在应用目录下创建一个文件夹templates,这是django默认搜索的模板文件夹名,在此目录下在创建一个myapp目录。
然后在创建两个模板html文件
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'myapp:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
</body>
</html>
接下来修改urls.py主要是指定index.html中{% url %},url函数是通过path里面的name属性定义的,但是有多个应用命名重复就需要用应用名(app_name 命名空间)将它们区分开来。myapp/urls.py
![](https://img.haomeiwen.com/i4716162/6ab8ceb0c58149ad.png)
使用{% url %}好处,就是修改path()函数中的地址后是不需要对模板代码进行修改的,不然我们修改地址还需要对html进行修改。
接下来修改views.py来调用模板来显示html
from django.httpimport HttpResponse
from .modelsimport Question
from django.shortcutsimport render
from django.shortcutsimport get_object_or_404
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list, }
return render(request, 'myapp/index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'myapp/detail.html', {'question': question})
![](https://img.haomeiwen.com/i4716162/5047ed49163728c5.png)
以上就是需要修改的文件,然后我们刷新页面(这些改动不用停止服务器也会自动刷新 自己测的时候是这样)。
![](https://img.haomeiwen.com/i4716162/88a16c662a33206a.png)
点击某个标签后
![](https://img.haomeiwen.com/i4716162/a60ab6fc6db435d3.png)
接下来修改下detail.html加入投票功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ question.question_text }}</h1>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'myapp: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="投票"/>
</form>
</body>
</html>
这里使用了form标点提交, {% csrf_token %}内容查看官方说明。
然后对应编写views.py里的vote函数
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):
return render(request, 'myapp/detail.html', {
'question': question,
'error_message':"You didn't select a choice.",
})
else:
selected_choice.votes = F('votes') +1
selected_choice.save()
return HttpResponseRedirect(reverse('myapp:results', args=(question.id,)))
表单提交后会回调此函数,首先判断是否存在该选项,如果未找到会抛异常KeyError。
找到责对choice的votes字段进行累加操作,这里的F()函数会防止线程安全,保证数据安全。
接下来处理results函数,这里显示最终投票后的结果,并且加入返回首页和继续投票的选项。
results.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<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 'myapp:detail' question.id %}">再次投票?</a>
<a href="{% url 'myapp:index'%}">返回问题首页?</a>
</body>
</html>
results函数就比较简单
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'myapp/results.html', {'question': question})
看下运行结果↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
![](https://img.haomeiwen.com/i4716162/2c93a7ac69ae8ee5.png)
![](https://img.haomeiwen.com/i4716162/cf8e15ba04831df9.png)
![](https://img.haomeiwen.com/i4716162/5ccac6fb222baecc.png)
以上就是官方介绍的简单实例,自己跑了一遍感觉很方便,比如做个喜好收集,或者意见调查等小应用,用django是不是太方便啊
![](https://img.haomeiwen.com/i4716162/d7eca2816d1e73b0.png)
。
网友评论