1. Custom page
Write a page that shows the current time
views.py file plus a module using the template, the overall content is:
#Coding=utf-8 fromDjango.shortcutsImportRender fromDjango.template.loaderImportget_template fromDjango.templateImportContextImportdatetime#Import SYS#Reload (SYS)#sys.setdefaultencoding (' cp936 ')#Create your views here. fromDjango.shortcutsImportRender,render_to_response fromDjango.httpImportHttpResponse#Create your views here.A="Test"defHello (Request):returnHttpResponse (a)defCurrent_datetime (Request): now=Datetime.datetime.now () T= Get_template ('current_datetime.html') HTML= T.render (Context ({'current_date': now})) returnHttpResponse (HTML)
Where current_datetime.html is the template we want to use, you can add a lot of effects, the basic content of
<!DOCTYPE HTML><HTMLLang= "en"> <Head> <MetaCharSet= "UTF-8"> <Metaname= "Generator"content= "editplus®"> <Metaname= "Author"content=""> <Metaname= "Keywords"content=""> <Metaname= "Description"content=""> <title>Document</title> </Head> <Body>It's now {{current_date}}</Body></HTML>
Where current_date is the exact switch that corresponds to a data conversion
urls.py content naturally also add a module-bound statement
fromDjango.conf.urlsImportpatterns, include, url fromDjango.contribImportAdmin fromViews.viewsImportHello fromViews.viewsImportCurrent_datetimeurlpatterns= Patterns ("', #Examples: #URL (r ' ^$ ', ' MyDjango.views.home ', name= ' home '), #URL (r ' ^blog/', include (' Blog.urls ')), #URL (r ' ^admin/', include (Admin.site.urls)),URL (r'^hello/$', hello), url (r'^current_datetime/$', Current_datetime),)
After this, the browser input http://127.0.0.1:8000/current_datetime/, will be error
Templatedoesnotexist at/current_datetime/
One of the words is using loader django.template.loaders.app_directories. Loader:
D:\Python34\lib\site-packages\django\contrib\admin\templates\current_datetime.html (File does not exist)
D:\Python34\lib\site-packages\django\contrib\auth\templates\current_datetime.html (File does not exist)
It is obvious that the template file should be placed under one of the two folders and move the file
Then we add such a sentence in the settings.py, which must be placed behind the Base_dir:
Template_dirs= (Os.path.join (Base_dir,'templates'),)
Problem solving
How to locate Basedir? I used the stupid way, setting.py has a sentence configuration:
' NAME ' ' Db.sqlite3 '),
I found the Db.sqlite3 catalogue.
There should be another way, welcome to explore, the next research database or AUTH certification.
Windows django1.7 +python3.4.2 build record 2