设为首页 加入收藏

TOP

frist Django app — 三、 View(一)
2017-09-30 16:52:47 】 浏览:8317
Tags:frist Django app View

 前面已经说过了Django中model的一些用法,包括orm,以及操作的api,接下来就是搭一些简单的界面学习view——Django中的view。主要介绍以下两个方面:

  • url映射
  • 请求处理
  • 模板文件

url映射和请求处理共同构成了MVC中的C——controller,这里面涉及了url的写法(主要是正则表达式),接收页面传递过来的参数。在Django中可以使用template来进行页面渲染,这样可以直接使用传递过来的数据。

url映射

这里紧接着上一篇的工程完善polls项目,polls网站首页显示最近的问题列表,还可以查看某一个问题的详情,可以进一步查看调查问题的结果。进行投票

  • index:显示最近的问题列表
  • detail:查看调查问题详细信息
  • result:显示某一个问题的结果
  • vote:对问题某一个答案进行投票

以上的四个功能我们分别用四个页面,编辑polls/urls,py

from django.conf.urls import url
from . import views

app_name = 'polls'
urlpatterns = [
    url(r'^$', views.index, name = 'index'),
    url(r'^(?P<question_id>[0-9]+)/detail/$', views.detail, name = 'detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name = 'results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name = 'vote'),
]

name:url规则的名称,可在模板中引用

app_name:定义url的命名空间,命名空间的作用就是防止name重名

请求处理

编辑polls/views.py

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from models import Question

# Create your views here.
def index(request):
   # 查询最新的五条Question,- 表示降序排列 latest_question_list
= Question.objects.order_by('-publ_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/detail.html', {'question': question}) def results(request, question_id): return HttpResponse("result:question id is %s" % question_id) def vote(request, question_id): return HttpResponse("vote:question id is %s" % question_id)

views.py处理通过urls.py分发过来的请求,比如上面最后一个vote中,很简单直接返回一个HttpResponse对象,其实所有的views处理都是返回该HttpResponse对象,包括使用能够render方法的时候

  • 直接返回HttpResonse
  • 通过调用render,将数据渲染到templates再返回HttpResponse

get_object_or_404:获取一个Question对象,如果没有就抛出Http404 Exception,这是经过优化后的代码,本来是自己手动查找一个对象,自己判断对象是否存在,不存在再raise Exception,改进之后降低了model和view之间的耦合,这样view不再依赖model层的具体实现,达到解耦的目的。

模板文件

在mysite/polls下新建两级目录mysite/polls/templates/polls,templates是固定的,polls是APP名称,然后在mysite/polls/templates/polls目录下新建一个index.html文件——也就是模板文件

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls.detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

这里有几点先说明一下:

templates/polls目录

在settings.py里面有关于templates的配置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

BACKEND:后端用的模板引擎django.template.backends.django.DjangoTemplates

DIRS:模板的位置也就是说不一定要放在约定的目录里面,但是建议最好放在每个APP名称的目录下,避免多个APP的情况下有模板名称相同的时候Django分不清

APP_DIRS:为true的时候DjangoTemplates会搜索app所在目录下的模板即polls/templates/polls

所以上面我们放在了默认目录

template语法

在Djang的模板里面包含普通的h

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python IO多路复用 下一篇python黑魔法 -- 内置方法使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目