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,)))
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
class QuestionDetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
1. django是什么?作用?
django是什么? Django是开源的Python Web框架,用于构建安全的、可靠的、可重复利用的web框架。 作用? 帮助开发中快速的构建web应用,提供了基础的web框架服务,使得开发者只需要关注自身的业务功能开发。
2. django项目构建流程
这里以demo项目mysite的构建为例主要流程如下(在win10环境):
2.1 安装django
在cmd窗口下,使用指令
pip install Django
安装django,安装完之后,使用指令py -m django --version
查看安装情况,执行结果如下: 说明:如果类似上图输出,说明django框架已经安装成功了。2.2 创建项目
在Django框架中,项目和应用的构建都是使用命令行构建的;接下来就是创建项目mysite,执行指令如下:
django-admin startproject mysite
执行之后就会创建如下的文件目录: 项目目录结果说明:接下来,运行指令
py manage.py runserver
启动项目,如下图所示: 执行之后如果如上图所示,说明项目创建成功了。2.3 创建应用程序
一般一个项目会有会有多个应用程序(模块或子系统),接下来看看Django是如何创建应用程序的,执行指令
py manage.py startapp polls
(执行指令的目录需要和manage.py在同一个目录下),创建结果如下: 应用程序polls目录结构说明:2.4 配置数据模型和建库
数据模型是一个业务系统中最关键的一部分,是系统的核心,这里主要有两个数据模型,分别是Question和Choice。一个Question对应多个Choice,两者是1:m的关系,在polls/models.py中配置如下:
接下来执行指令
py manage.py migrate
来执行数据库模型的构建,如下图所示:说明:其中polls_question和polls_choice是自定义的模型,其他均是Django框架自带的数据模型。
2.5 配置admin模块
创建超级管理员,执行指令
py manage.py createsuperuser
来创建。创建成功后在 http://localhost:8000/admin/中,便可以登录管理用户等信息。将自定义的数据模型添加到admin中,在polls/admin.py添加如下代码:
admin.site.register(Question) admin.site.register(Choice)
from django.urls import path from . import views
app_name = 'polls' # 添加应用命名空间 urlpatterns = [
ex: /polls/
]
from django.contrib import admin from django.urls import path,include
urlpatterns = [ path('admin/', admin.site.urls), path('polls/',include('polls.urls')), #将应用路由信息配置到项目中 ]
{% load static %}
{% if latest_question_list %}
{% for question in latest_question_list %} {#- {{ question.question_text }}
#}
- {{ question.question_text }}
{% endfor %}
{% else %}
No polls are available.
{% endif %}
{{ question.question_text }}
{% if error_message %}
{{ error_message }}
{% endif %}{{ question.question_text }}
{% for choice in question.choice_set.all %}- {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
{% endfor %}
Vote again?
{% load static %}
1.在Django中,views起到接口跳转和页面跳转的逻辑,处理请求的响应。有点类似spring mvc中controller的机制。在polls/views.py中代码如下: from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse, Http404, HttpResponseRedirect from django.template import loader
Create your views here.
from django.urls import reverse from django.utils import timezone from django.views import generic
from polls.models import Question, Choice
def index(request):
mvt
return render(request, 'polls/index.html', context)
def detail(request, question_id): try: question = Question.objects.get(pk=question_id) except Question.DoesNotExist: raise Http404("Question does not exist") return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': 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.
class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_question_list'
class QuestionDetailView(generic.DetailView): model = Question template_name = 'polls/detail.html'
class ResultsView(generic.DetailView): model = Question template_name = 'polls/results.html'