• django入门-视图-part3


    尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6511384.html 

    完全翻译自官方文档 https://docs.djangoproject.com/en/1.10/intro/tutorial03/

    本节讲视图

    我们设计一个简单投票页面,它有如下3个功能

    1. 显示要投票的问题

    2. 点击"投票"按钮提交你的选项

    3. 展示这个投票的结果

    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)  # 提交投票选项内容

    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'),
        # 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'),
    ]

    一个视图只会返回 HttpResponse 或 Http404

    下面写一个从数据库获取内容的view

    from django.http import HttpResponse
    
    from .models import Question
    
    
    def index(request):
        latest_question_list = Question.objects.order_by('-pub_date')[:5]
        output = ', '.join([q.question_text for q in latest_question_list])
        return HttpResponse(output)

    我们在代码里硬编码了页面只返回5条内容,这很外行,所以我们用django的模板系统

    django默认会在这个目录结构下读取我们的模板文件

    polls/templates/polls/index.html

    就是在你的应用目录下创建文件夹 templates ,再在templates里创建一个跟应用名字一样的目录poolls

    这个index.html的访问路径就是 polls/index.html

    你的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 %}

    相应的修改你的views.py

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

    context 就是一个字典, 模板里会使用这个变量

    上面的方法稍显麻烦,先加载loader模板文件,再渲染render文件,django为我们提供了一个更方面的命令 render()

    继续修改我们的使用,我们使用render重写上面的index()

    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)

    当访问的页面不存在时,我们抛一个404出去

    from django.http import Http404
    from django.shortcuts import render
    
    from .models import Question
    # ...
    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})

    再精简代码,正常获取和抛404可以用一个方法 get_object_or_404()

    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})

    类似的还有get_list_or_404(),还有最好用filter()函数代替get(),因为get()会抛一个Http404异常

    模板系统开讲

    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>

    用点来访问变量的属性

    来看这段代码,我们在href中硬编码了访问路径

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

    怎么解决呢,在url()方法里加name参数

    ...
    # the 'name' value as called by the {% url %} template tag
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    ...

    修改以后,上面的硬编码变成这样

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

    这样即使你修改了你的url路径,那么你的html也不需要修改.

    URL命名空间

    在一个django项目里可能包含很多应用, 那么就会出现多个url(name="这里的名字有重复"),那么怎么解决呢

    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]+)/$', 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'),
    ]

    同时修改你的html

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

    看到区别了吗

    好了 本节完

  • 相关阅读:
    存储过程之六—触发器
    存储过程之五—条件和异常处理
    存储过程之四—游标
    json的那些事
    聊聊js中的typeof
    JavaScript各种继承方式和优缺点
    两边宽度已知,如何让中间自适应
    html5笔记——<section> 标签
    vue实现仿淘宝结账页面
    vue2.0在table中实现全选和反选
  • 原文地址:https://www.cnblogs.com/txwsqk/p/6511384.html
Copyright © 2020-2023  润新知