File directory:
1. Other file configurations for the same project:
In the file: settings.py in this add
Installed_apps = (
' Django.contrib.admin ',
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.messages ',
' Django.contrib.staticfiles ',
' MyTest ', #这是新添加的文件
)
2. Template Configuration
(1) Add template folder path: Add the Templates template file under MyTest (where the ' Templates ' named template, because it already exists in Django, the same time, the template file you added does not take effect, the access will be error, the corresponding file cannot be found
Add in settings.py:
From Os.path Import Join
Template_dirs = (
Join (Os.path.dirname (__file__), ' mytest\\templates '),
)
Use: In the views.py file for example:
# Loading templates
From django.shortcuts import Render_to_response, Render
def template_test (Request):
# return render (Request, ' 2.html ', {' name ': ' Hello '})
Return Render_to_response (' 2.html ', {' name ': ' Test_template '})
3.URL configuration:
such as accessing the address in the Web http://localhost:8000/hello/test1/
Add in URL:
From mytest.views Import *
Urlpatterns = [
URL (r ' ^hello/$ ', hello),
URL (r ' ^hello/(\d+)/', hello1),
URL (r ' ^hello/a/', template_test),
URL (r ' ^hello/test1/', Template_test1), # Accessed when matching is this line
URL (r ' ^admin/', include (Admin.site.urls)),
]
Template_test1 is the function inside the mytest.views as follows:
def template_test1 (Request):
# return render (Request, ' 2.html ', {' name ': ' Hello '})
Return Render_to_response (' 4.html ')
Templates, URLs, the same project other file configurations