Django removing hardcoded URLs in template---Use variables to place URLs in variables {% url ' namespace:name '%}

Source: Internet
Author: User

1 urlpatterns = [2     url (r'^admin/', Admin.site.urls),  3     url (r'^votes/', include ("polls.urls ", namespace="polls")),4 ]
/myproject/urls.py

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 " "5 @version:6 @author: Leo Yang7 @license: None8 @contact: [email protected]9 @site:Ten @software:P ycharm One @file: urls.py A @time (utc+8): 16/7/29-12:28 - " " -  the  fromDjango.conf.urlsImportURL -  fromDjango.contribImportAdmin -  from.Import views -  +Urlpatterns = [ -URL (r'^admin/', Admin.site.urls), +URL (r'^index/', Views.index, Name="Index"), AURL (r'^detail/', Views.detail, Name="Detail"), atURL (r'^choice/', Views.choice, Name="Choice"), -URL (r'^result/', Views.result, Name="result"), -]
/projectname/polls/urls.pyy

1<! DOCTYPE html>2"en">34<meta charset="UTF-8">5<title>this isIndex page</title>67<body>8<li><a href="{% url ' polls:index '%}">{% URL'Polls:index'%}</a> </li>9<li><a href="{% url ' polls:detail '%}">{% URL'Polls:detail'%}</a> </li>Ten<li><a href="{% url ' polls:choice '%}">{% URL'Polls:detail'%}</a> </li> One<li><a href="{% url ' polls:result '%}">{% URL'Polls:result'%}</a> </li> A<li><a href="http://www.baidu.com">url:www.baidu.com</a></li> -  -</body> the/projectname/polls/templates/index.html

1  fromDjango.shortcutsImportRender2  fromDjango.httpImportHttpResponse3  fromDjango.shortcutsImportrender,get_object_or_404,get_list_or_4044 5 #Create your views here.6 7 8 defIndex (Request):9Ret_render = render (Request,"polls/index.html")Ten     returnRet_render One     returnHttpResponse ("The IS index page") A  -  - defDetail (Request): the     returnHttpResponse ("This is detail page") -  -  - defChoice (Request): +     returnHttpResponse ("This is choice page") -  +  A defresult (Request): at     returnHttpResponse ("This is the result page")
/profectname/polls/views.py

Django removing hardcoded URLs in template---Use variables to place URLs in variables {% url ' namespace:name '%}

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.