I am about to get off work. Today I have studied Japanese translation for a whole day (my project is closely related to Japanese). I am so tired that I will secretly summarize Django I learned before I leave work.
Because it cannot take a lot of time to learn Django, it needs to be balanced ~ So today we made a template.
Because of the view described in [1] In the previous writing, it is natural to start to work as a template today.
What is a template?
Anyone who has played C ++ knows (c ++ Three Treasures: template, stl, class), but to make the blog seem to have been written by the author, I will explain it in a simple way: we hate introducing ourselves during interviews (as if the interviewer does not read his resume ?), For this reason, I can only: My name is XXX, from XXX, special XXX. In my "self" introduction, the template is almost the same.
"My name is XXX, from XXX, special XXX" is a self-introduction "template". Each time we just need to fill in the most basic information.
What is the role of the template?
In fact, I think this problem seems to be of little significance, the role of the template is to reduce repetitive work, for example, you do A series of similar projects (such as A + B, A * B, A-B, a/B), you can understand the meaning of the template by YY.
Template example
Create today's project, django_admin.py stratproject worldtime
The example we are doing today is to create a web page that simply shows the time
Before that, we need to use a template because I am a superficial person (so my blog is not popular ~ Only you are watching)
So what we do is very simple. Find manage. py in our project worldtime.
Python manage. py shell
Go to a familiar python interactive interface, but python doesn't work ~!
I heard this is a stupid method ~ That's it!
First, import the package
from django import template
Then we define our template
t = template.Template('My name is {{ name }},come from {{ province }},hobby is {{ hobby }}')
Note that {} is what we want to fill in.
Let's construct and refill it. The author shamelessly uses his own example ~
c=template.Context({'name':'yaolong','province':'Guangdong','hobby':'program'})print t.render(c)
The result is:
My name is yaolong,come from Guangdong,hobby is program
Now, let's start writing views. py again, just like in the previous section.
This time, we define a worldtime function.
#! /Usr/bin/env python # coding = UTF-8 #**************************** * ************************** #> OS: linux 3.11.10-301. fcw.x86 _ 64 Fedora20 #> Author: yaolong #> Mail: dengyaolong@yeah.net #> Time: friday, June 13, 2014 #******************************* * ************************ from django. http import HttpResponseimport datetimedef worldtime (request, offset): try: delt = int (offset) failed t ValueError: delt = 0 cur_time = datetime. datetime. now () deltaH = datetime. timedelta (hours = delt) cur_time + = deltaH html = "
It's embarrassing to use some very boring functions and capture exceptions.
To explain, datetime. datetime. now () is used to obtain the current time, but it is too accurate! It's not easy to understand. You can use a regular expression. I miss shell # {}~ all the time #{}~
Create a time difference, add up the output, and notice a very frustrating html
Then, configure it in urls. py!
from django.conf.urls import patterns, include, urlfrom django.contrib import adminadmin.autodiscover()from views import worldtimeurlpatterns = patterns('', # Examples: # url(r'^$', 'worldtime.views.home', name='home'), # url(r'^blog/', include('blog.urls')), #url(r'^admin/', include(admin.site.urls)), (r'^time/([0-9]{0,2})',worldtime) ,)
Regular Expressions refer to http: // 127.0.0.1: 8000/time/+ 0 ~ The number of 9 appears 0 ~ Twice. Parentheses are used to pass to offset.
Wait! It seems that no template has been seen! The question is gone!
But it's coming to work! Holiday is coming soon! I'm so excited! Leave a version with no responsibility.
#! /Usr/bin/env python # coding = UTF-8 #**************************** * ************************** #> OS: linux 3.11.10-301. fcw.x86 _ 64 Fedora20 #> Author: yaolong #> Mail: dengyaolong@yeah.net #> Time: friday, June 13, 2014 #******************************* * ************************ from django. http import HttpResponsefrom django. template import Template, Contextimport datetimedef worldtime (request, offset): try: delt = int (offset) failed t ValueError: delt = 0 cur_time = datetime. datetime. now () deltaH = datetime. timedelta (hours = delt) cur_time + = deltaH t = Template ("
Run and pull! I will finish the cet6 test tomorrow and try again.
Things! It's really terrible to come to Shenzhen for four days. I want to go back to Guangzhou !!!!