Django Official Documentation 1.6 study notes-Write your first Django program <2>

Source: Internet
Author: User

  1. Write a few useful views

    Each view is responsible for doing two things, returning a HttpResponse object that contains the contents of the requested page, or throwing an exception, such as Http404. What the rest of the view does depends on you.

    Your view can read records from the database, or not read them. You can use a template system such as a Django-brought or a third-party Python templating system, or not. You can generate a PDF file in real time, output XML, and create a ZIP archive package. Everything you want to do can be done using any Python class library you want to use.

    For convenience, we use the Django-based database API, which allows us to simply look at the index () view, which shows the latest 5 comma-delimited questions sorted by publication time.

    From django.http import HttpResponse

    From Polls.models import Poll

    def index (Request):

    Latest_poll_list = Poll.objects.order_by ('-pub_date ' [: 5])

    Output = ', '. Join ([p.question for P in Latest_poll_list])

    return HttpResponse (Output)

    There is a problem here, the design of the page is hard coded in the view. If you want to change the appearance of the page, you will have to modify the code for this view. So we use the Django template system to create a view and page separation design by creating a template that can be used by the views.

    First, create a folder with the name templates in your polls folder. Django will look for templates from templates.

    The Django Template_loaders configuration contains a callable list that knows how to import templates from various sources. One of the default loaders is django.template.loaders.app_drectories. Loader, this loader looks for "templates" folders from subdirectories of each app installed in Intalled_apps, which is why Django knows how to find polls templates folders, even if we don't modify Template_ DIRS

    I can put all the templates in a large Templates folder, and this will work just as well. But these templates belong to the polls app, so unlike the admin app we created earlier templates, we'll put this template in the app's templates

    In the Templates folder that we just created, create another folder polls, under which Index.html is created under the polls folder. polls/templates/polls/index.html. Because the App_directories template loader works, you can reference it in Django in a polls/index.html way.

  2. Template namespaces:
    Now we can put our template directly into polls/templates instead of creating another polls subdirectory. But this frame is a bad idea. Django will select the first template that matches the name, and if you have a template with the same name in another app, Django will not be able to differentiate them from each other.

    We need to be able to find the template that we need, the simplest way is to name the template (namespace), that is, by putting these templates in another folder named the app itself.

    That's, by putting those templates inside another directory named for the application itself.

  3. Put the following code in Xie Index.html

    {%if latest_poll_list%}

    <ul>

    {%for poll in latest_poll_lis%}

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

    {%endfor%}

    {%else%}

    <p>no polls ate available</p>

    {%endif%}

  4. Now let's modify the code in the view below

    From django.http import HttpResponse

    From django.template import Requestcontext,loader

    From Polls.models import Poll

    def index (Request):

    Latest_poll_list = Poll.objects.order_by ("-pub_date") [: 5]

    Template = Loader.get_template ("polls/index.html")

    Content = RequestContext (request,{

    ' Latest_poll_list ': latest_poll_list,

    })

    Return HttpResponse (Template.render (context))

Django Official Documentation 1.6 study notes-Write your first Django program <2>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.