Using templates in a view under the Django framework

Source: Internet
Author: User
This article mainly introduces how to use templates in Views under the Django framework. Django is one of the most popular Python frameworks. For more information about how to open the current_datetime view, see. The following is the content:

from django.http import HttpResponseimport datetimedef current_datetime(request):  now = datetime.datetime.now()  html = "It is now %s." % now  return HttpResponse(html)

Let's use the Django template system to modify this view. Step 1, you may have thought of making the following changes:

from django.template import Template, Contextfrom django.http import HttpResponseimport datetimedef current_datetime(request):  now = datetime.datetime.now()  t = Template("It is now {{ current_date }}.")  html = t.render(Context({'current_date': now}))  return HttpResponse(html)

Yes, it does use the template system, but it does not solve the problems we have pointed out at the beginning of this chapter. That is to say, the template is still embedded in the Python code and does not truly implement the separation of data and performance. Let's place the template in a separate file and let the view load the file to solve this problem.

You may first consider saving the template somewhere in the file system and using the Python built-in file operation function to read the file content. If the file is saved in/home/djangouser/templates/mytemplate.html, the code will be as follows:

from django.template import Template, Contextfrom django.http import HttpResponseimport datetimedef current_datetime(request):  now = datetime.datetime.now()  # Simple way of using templates from the filesystem.  # This is BAD 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}))  return HttpResponse(html)

However, this method is not concise for the following reasons:

  • It does not deal with the loss of files. If the mytemplate.html file does not exist or is unreadable, the open () function call will cause an IOError.
  • The location of the template file is hard-coded here. If you use this technology in each view function, you need to constantly copy the positions of these templates. Not to mention a large amount of input work!
  • It contains a large number of annoying duplicate code. Instead of calling open (), fp. read (), and fp. close () every time a template is loaded, it is better to make a better choice.

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.