Python Learning (vi) Defining views and page templates

Source: Internet
Author: User

Request parsing is usually obtained by requesting a request for a certain parameter, and then according to the parameters of the business logic judgment, which may include querying the database, and then the need to return the data encapsulated into a httpresponse return.

The code is as follows:

This is a simple function to process the request, corresponding to the URL mapped before URL (R ' ^articles/([0-9]{4})/$ ', Views . Year_archive), Django passes the content wrapped in the URL as a variable to the function, where the year variable in year_archive is the value represented by ([0-9]{4}).

Article.objects.filter (pub_date__year=year) filters out data that is dated year. Note Pub_date__year, only the Pub_date field in the database table, Pub_date__year represents the value of the year.

So you can think of this as a form of interface query that Django provides by default.

The returned list and year are then assembled into a dictionary data.

The last call to the Django-provided function, Render returns. The thing to do with render is to choose a template, create a context object, and then put that data into a httpresponse created.

mysite/news/views.py fromDjango.shortcutsImportRender from. ModelsImportarticledefyear_archive (Request, year): A_list= Article.objects.filter (pub_date__year=Year ) Context= {' Year': Year,'article_list': A_list}returnRender (Request,'news/year_archive.html', context)

The code above actually encapsulates the following data, which encapsulates the role of the following code in render.

  

 fromDjango.templateImportTemplate, Context fromDjango.httpImportHttpResponseImportdatetimedefCurrent_datetime (Request): now=Datetime.datetime.now ()#Simple how to using templates from the filesystem.    #This is the because it doesn ' t account for missing files!fp = open ('/home/djangouser/templates/mytemplate.html') T=Template (Fp.read ()) fp.close () HTML= T.render (Context ({'current_date': now})) returnHttpResponse (HTML)

Python Learning (vi) Defining views and page templates

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.