Templates, URLs, the same project other file configurations

Source: Internet
Author: User

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

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.