Getting Started with Django (v) multilevel URLs and default URLs

Source: Internet
Author: User

Django We release the use of the APP01 application index.html, first need to Myjango subdirectories in the URLs file to edit the URL of the route 650) this.width=650; src= http://s2.51cto.com/ Wyfs02/m00/7d/54/wkiom1bmm9ujiej-aabntlmu1hw540.png "title=" 1.png "alt=" Wkiom1bmm9ujiej-aabntlmu1hw540.png "/>

This applies when there are fewer applications. Joining us is not only app01, but there are many more apps in the back. It is confusing to write these routes to the URLs in the subdirectory Mudjango directory. Using multilevel URLs at this point is a better solution.

First, create an application named APP02.

At the command line, enter

Python manager.py Startapp app02

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/52/wKioL1bmN__gYBARAAALkfzabtk778.png "title=" 1.png " alt= "Wkiol1bmn__gybaraaalkfzabtk778.png"/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/54/wKiom1bmN73T0sMlAAAlOYdTaxY332.png "title=" 1.png " alt= "Wkiom1bmn73t0smlaaaloydtaxy332.png"/>


The second step is to create two HTML files in the template directory for two apps. Also create a default.html file to test the default URL in a moment. The specific directory structure is as follows

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/54/wKiom1bmOgGDxr_gAAAjMzPDq0A835.png "title=" 1.png " alt= "Wkiom1bmoggdxr_gaaajmzpdq0a835.png"/>


Edit these three new HTML, because only the test can be separated by the three files. No simple code is listed here.


The third step, edit the views.py under the app01 application

views.py

#/usr/bin/env python# coding:utf-8from django.shortcuts import renderdef app01_index (Request): Return render (Request, ' App01/app01_inde.html ') def default_index (Request): Return render (Request, ' default.html ')


Add a new urls.py file under the app01 application module and write the following in the Mydjango sub-directory urls.py content

#!/usr/bin/env python#-*-coding:utf-8-*-from django.conf.urls import urlfrom django.contrib import Adminfrom app01 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), url (r ' ^app01index/$ ', views.app01_index), #定义默认访问路由 , which means enter any URL path url (r ' ^$ ', Views.default_index),]

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/53/wKioL1bmQvHReeybAACVNyHMLXk566.png "title=" 1.png " alt= "Wkiol1bmqvhreeybaacvnyhmlxk566.png"/>

The same way to edit the content in the app02 app

/app02/views.py

#/usr/bin/env python# coding:utf-8from django.shortcuts import renderdef app02_index (Request): Return render (Request, ' App01/app02_index.html ')


Edit app02 's urls.py

/app02/urls.py

#!/usr/bin/env python#-*-coding:utf-8-*-from django.conf.urls import urlfrom django.contrib import Adminfrom app02 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), url (r ' ^app02index/$ ', Views.app02_index),]

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/54/wKiom1bmQF7AH0RbAACLdhyWUb8483.png "title=" 1.png " alt= "Wkiom1bmqf7ah0rbaacldhywub8483.png"/>


The fourth step, is the most critical step, we now edit the Mydjango subdirectory under the urls.py

mydjango/urls.py

#!/usr/bin/env python# coding:utf-8# Here an include module is referenced from django.conf.urls import Url,includefrom Django.contrib Import Admin "" "Here is to tell the routing system, encountered multi-level URL when to go to which app to find the corresponding sub-url" "" Urlpatterns = [url (r ' ^admin/', admin.site.urls), #因为是多级的url , be sure to remove the ' $ ' number. Because this is only the first level of the URL, followed by a two-level URL URL (r ' ^/app01/', include (' App01.urls ')), the URL (r ' ^/app02/', include (' App02.urls ')), #如果域名后 The polygon does not specify a path to match this rule, here also to remove the ' $ ' number url (r ' ^ ', include (' App01.urls ')),]

Finally, start the server test to see if our configuration was successful.

Let's test the APP01 first.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/57/wKiom1bmXTiC-GR3AABl30Rfaek137.png "title=" 1.png " alt= "Wkiom1bmxtic-gr3aabl30rfaek137.png"/>

You can see that this page went through the path of APP01 before entering the App01_index


Same APP02 is the same effect

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/55/wKioL1bmXpWAZZNaAABmKiTzgow054.png "title=" 1.png " alt= "Wkiol1bmxpwazznaaabmkitzgow054.png"/>


Finally, to check the default home page, do not enter any URL path when it can be displayed correctly

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/55/wKioL1bmXunQq8B7AABTvirePt4281.png "title=" 1.png " alt= "Wkiol1bmxunqq8b7aabtvirept4281.png"/>


The configuration of the multilevel URL and the default URL is implemented here.

This article from "Thunderbolt Tofu" blog, declined reprint!

Getting Started with Django (v) multilevel URLs and default URLs

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.