啊峰哥

4 Django应用 第3部分(视图部分)

接着昨天写的那篇笔记,今天继续学习DJango中的内容。这一章主要是介绍Django中的视图部分。

 

4.1视图理念

4.2编写第一个视图

4.3编写更多的视图

4.4给视图编写功能

4.5render()和404错误

4.6使用模版

4.7使用url命名空间

 

4.1视图理念

视图:视图(view)是Django应用中的一“类”网页,它通常使用一个特定的函数提供服务。

视图通过使用特殊的函数,传递网页页面和其他内容,同时,视图也对web请求进行回应。通过检查请求的url选择使用哪一个视图。

4.2编写第一个视图

first,

打开polls/views.py,开始编写视图函数

polls/views.py

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

 

 

second,

要在polls目录下创建一个URLconf,创建url.py文件

目录:

polls/
    __init__.py
    admin.py
    models.py
    tests.py
    urls.py
    views.py

同时键入代码,使网址和视图函数连接起来

polls/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

 

third,

在polls/urls.py中键入URLconf, 使得url可以连接到polls.urls模块

mysite/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

 

结果,

将Index视图关联成功,浏览http://localhost:8000/polls/,会出现在视图中定义的网页内容。

 

解释:url()参数:(regex,view,*kwagrs,*name)

regex:正则表达式的短格式,匹配满足字符串的网页

view:找到一个正则表达时,就会调用view作为视图函数

kwargs:任何关键字参数都可以以字典形式传递给目标视图,暂不用

name:给URL命名,可以通过名称来引用URL

4.3编写更多的视图

first,编写更多的视图函数

polls/views.py

#。。。
def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

second,编写url()将函数和URL连接起来

polls/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),  #?P<question_id> 定义一个名字,它将用于标识匹配的模式
    # ex: /polls/5/results/
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

打开浏览器,输入“/34/”它将运行detail()方法并显示你在URL中提供的ID。 再试一下“/polls/34/results/”和“/polls/34/vote/” —— 它们将显示出对应的结果界面和投票界面。

 

解释:

  • Django发现匹配到了正则表达式'^polls/'

  • 然后,Django将去掉匹配到的文本("polls/")并将剩下的文本 —— "34/" —— 发送给‘polls.urls’ URLconf 做进一步处理,这时将匹配r'^(?P<question_id>[0-9]+)/$'并导致像下面这样调用detail()视图:

  • detail(request=<HttpRequest object>, question_id='34')
    
    

     

    4.4给视图编写功能

    first,视图从数据库中读取记录

    polls/views.py
    
    from django.http import HttpResponse
    
    from .models import Question
    
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        output = ', '.join([p.question_text for p in latest_question_list])
        return HttpResponse(output)
    
    # Leave the rest of the views (detail, results, vote) unchanged

     

    second,创建模版

    这时,页面设计被硬编码在视图中,我们使用模版,使页面和视图分离出来

    在polls目录下创建templates目录,然后在templates目录下,创建另外一个目录polls,最后在其中创建文件index.html。为了避免与其他应用模版重复。

    模版位置:(polls/templates/polls/index.html

    放入模版文件

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

    更新视图

    polls/views.py
    
    from django.http import HttpResponse
    from django.shortcuts import render
    
    from .models import Question
    
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        context = {'latest_question_list': latest_question_list}
        return render(request, 'polls/index.html', context)

    render()函数将请求对象作为它的第一个参数,模板的名字作为它的第二个参数,一个字典作为它可选的第三个参数。 它返回一个HttpResponse对象,含有用给定的context 渲染后的模板。

     

    third,处理视图函数,引发一个404错误

    polls/views.py

    from django.shortcuts import get_object_or_404, render
    
    from .models import Question
    # ...
    def detail(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        return render(request, 'polls/detail.html', {'question': question})
    
    polls/templates/polls/detail.html
    
    {{ question }}

    解释:如果没有根据ID找到一个Question对象,就会引发404错误

     

    4.5使用模版和url命名空间

    浏览http://localhost:8000/polls/,会出现在视图中定义的网页内容。

    更改页面,detail.html模版应该是这样子的

    polls/templates/polls/detail.html
    
    <h1>{{ question.question_text }}</h1>
    <ul>
    {% for choice in question.choice_set.all %}
        <li>{{ choice.choice_text }}</li>
    {% endfor %}
    </ul>

     

    更改index.html,改变一部分硬编码

     

    <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

    更改为

    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

    它的工作原理是在polls,urls模版中查找指定的URL,因为刚才给一个URL命名为

    detail,所以可以直接调用。

     

    还有一个问题。如果应用特别多,不同的应用可能拥有几个名为detail的视图。

    这个时候,在主URLconf下添加命名空间,可以防止这个问题

    首先修改urls.py

    mysite/urls.py
    
    from django.conf.urls import include, url
    from django.contrib import admin
    
    urlpatterns = [
        url(r'^polls/', include('polls.urls', namespace="polls")),
        url(r'^admin/', include(admin.site.urls)),
    ]

    其次将

    polls/templates/polls/index.html
    
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

    修改为    【浏览http://localhost:8000/polls/,会出现在视图中定义的网页内容。】

    polls/templates/polls/index.html
    
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

posted on 2017-10-23 21:55  啊峰哥  阅读(357)  评论(0编辑  收藏  举报

导航