Django tutorial part3

writing_first_django_app_part3

在Django中,网页和其他内容都是通过views来呈现的,每个view由一个简单的python函数来表示,django通过检查url来选择一个view

URL pattern: URL的简单通用形式, eg: /newsarchive/<year>/<month>/

python用'URLconfs'来将URL patterns匹配到views

先看一个简单view的例子

# polls/view.py

from django.http import HttpResponse

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

我们需要将它映射到一个URL才能访问到它,这里我们需要一个URLconf 要在polls的目录下创建一个URLconf,首先要创建一个文件urls.py,加入代码:

from django.conf.urls import patterns, url

from polls import views

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

下一步要在根URLconf中插入这个新建的URLconf

# mysite/urls.py

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

修改之后重启服务器,访问localhost:8000/polls/就可以看到hello world的界面. 刚才添加一个view的流程就是先在poll/views.py添加新的view,然后在polls/urls.py创建一个新的URLconf,最后在根的mysite/urls.py中把它include进去

现在再添加多几个views

# polls/views.py

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def index(request):
    return HttpResponse("hello, world. You're at the polls index.")

def detial(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 patterns, url

from polls import views

urlpatterns = patterns('',
        # /polls/
        url(r'^$', views.index, name = 'index'),
        # eg: /polls/5/
        url(r'^(?P<question_id>\d+)/$', views.detail, name = 'detail'),
        # eg: /polls/5/results/
        url(r'^(?P<question_id>\d+)/results/$', views.results, name = 'result'),
        # eg: /polls/5/vote/
        url(r'^(?P<question_id>\d+)/vote/$', views.vote, name = 'vote'),

        )

因为前面已经在根目录的urls.py中添加polls的URLconf,所以不用再次添加 (?P<question_id>\d+)表示提取匹配\d+的内容,然后赋值到关键字question_id中作为参数传入到类里面去处理

每个view都会有两种功能中的其中一种,返回一个HttpResponse或者Http404.

现在我们尝试给view添加真正的功能,在index界面展示最新的5个poll questions,根据时间排序:

# polls/views.py

from django.http import HttpResponse

from polls.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

我们希望用自己的模板来显示Question列表,在polls目录创建目录templates,并在里面创建目录polls,在里面建立文件index.html:

# mysite/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 %}

这里把每个Question当作一个链接来展示,

然后修改view的代码来使用这个模板:

# polls/views.py

from django.http import HttpResponse
from django.template import RequestContext, loader

from polls.models import Question


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

上面使用了template.loader来调用模板,然后创建一个RequestContext对象来发出请求,经过模板渲染后返回HttpResponse对象

render()函数

第一个参数是request对象,第二个参数是模板名字,第三个参数可以选择一个字典,用来传递一些关键字参数.返回一个将请求内容渲染过后的HttpResponse对象

Raising a 404 error
# polls/views.py

from django.http import Http404
from django.shortcuts import render

from polls.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})

暂时性的detail.html:

# polls/templates/polls/detail.html

{{ question }}

上面代码在找不到这个Question对象的时候会返回404错误 也可以用get_object_or_404()函数来一次性处理

from django.shortcuts import get_object_or_404, render

from polls.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})

这样可以直接在获取对象的时候检验是否成功,否则就返回错误

Use the templates system
# 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>

模板系统可以通过'.'来访问变量的属性,如question.question_text

Namespacing URL names

目前只有一个应用的时候,URL的名字不会发生冲突,但是如果应用数量增加到多个,可能有一个应用有相同的URL名字,这时候可以通过加上作用域来解决:

# mysite/urls.py

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

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
)

现在将polls/templates/polls/index.html改为:

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

转载于:https://www.cnblogs.com/jolin123/p/4339975.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值