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