博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django doc summary (4)
阅读量:4317 次
发布时间:2019-06-06

本文共 3783 字,大约阅读时间需要 12 分钟。

View

In Django, each view is represented by a simple python function.

To get from a URL to view, Django uses what are know as 'URLconfs'. A URL conf maps URL patterns to views.

Now we edit the samapp views.py to add three functions.They are 'detail', 'results' and vote'.

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)

Then we should edit the 'samapp/urls.py' file to take the three functions in action.

# /samapp/url(r'^$', views.index, name='index'),# /samapp/23/url(r'^(?P
[0-9]+)/$', views.detail, name='detail'),# /samapp/23/results/url(r'^(?P
[0-9]+)/results/$', views.results, name='results'),# /samapp/23/vote/ url(r'^(?P
[0-9]+)/vote/$', views.vote, name='vote'),

A view that actually do something

views.py

def index(request):    latest_question_list = Question.objects.order_by('-pub_date')[:5]    output = ', '.join([q.question_teext for q in latest_question_list])    #return HttpResponse("Hello, this is sam site.")    return HttpResponse(output)

Template system

Next, let's use Django's template system to separate the design from python by creating a template that the view can use.

The template file position should like below.

├─samapp│  ││  ├─templates│  │  └─samapp│  │          index.html

index.html

{% if latest_question_list %}    
{% else %}

No polls are available.

{% endif %}

Then, we update the samapp/views.py to use the template.

def index(request):    latest_question_list = Question.objects.order_by('-pub_date')[:5]    template = loader.get_template('samapp/index.html')    context = RequestContext(request, {'latest_question_list': latest_question_list})    return HttpResponse(template.render(context))

A shortcut: render()

from django.shortcuts import renderfrom .models import Questiondef 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)

A shortcut: get_object_or_404()

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

Removing hardcoded URLs in templates

As before, in the template, the link war partially hardcoded link this:

    {
{ question.question_text }}

That is tightly-coupled approached with url '/samapp/'.So, we'd better do like this.

  • {
    { question.question_text }}
  • This will look up the URL definition in the samapp/urls.py file. And if you want to change the URL, you would change only int the samapp/urls.py file.

    Namespacing URL names

    When we have more than one apps, we will encounter a problem.

    The url name may be the same. So, we should add namespaces to our URL conf.

    from django.conf.urls import urlapp_name = 'polls'urlpatterns = [    url(r'^$', views.index, name='index'),    url(r'^(?P
    [0-9]+)/$', views.detail, name='detail'), url(r'^(?P
    [0-9]+)/results/$', views.results, name='results'), url(r'^(?P
    [0-9]+)/vote/$', views.vote, name='vote'),]

    Now, change the template from the origin to the below.

  • {
    { question.question_text }}
  • 转载于:https://www.cnblogs.com/samrui/p/5037573.html

    你可能感兴趣的文章
    解密zend-PHP凤凰源码程序
    查看>>
    python3 序列分片记录
    查看>>
    Atitit.git的存储结构and 追踪
    查看>>
    atitit 读书与获取知识资料的attilax的总结.docx
    查看>>
    B站 React教程笔记day2(3)React-Redux
    查看>>
    找了一个api管理工具
    查看>>
    Part 2 - Fundamentals(4-10)
    查看>>
    使用Postmark测试后端存储性能
    查看>>
    NSTextView 文字链接的定制化
    查看>>
    第五天站立会议内容
    查看>>
    CentOs7安装rabbitmq
    查看>>
    (转))iOS App上架AppStore 会遇到的坑
    查看>>
    解决vmware与主机无法连通的问题
    查看>>
    做好产品
    查看>>
    项目管理经验
    查看>>
    笔记:Hadoop权威指南 第8章 MapReduce 的特性
    查看>>
    JMeter响应数据出现乱码的处理-三种解决方式
    查看>>
    获取设备实际宽度
    查看>>
    Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
    查看>>
    Alpha冲刺(10/10)
    查看>>