django url aliases and reverse DNS namespace

url aliases and reverse lookup

  1. We usually write the url names are dead, if the project is too large, need to project a file name change it, change it so it is not a general trouble, so we have to play when the definition of an alias url future regardless of which file to use with its aliases are so much more convenient, no matter how you change the name of the path will not affect your operation

  2. How do you play url aliases

    url(r'^home1/',views.home,name='home'),
    # 像这样,在url后边加一个name叫做home,之后再其他的文件中需要引用这个url就可以直接使用别名
    
    url(r'^home1/(\d+)',views.home,name='home'),
    # 如果分组传参数的这种url要怎么去应用呢
  3. Use of an alias ( reverse analysis )

    #在文件中使用时,我们需要导入一个django的模块,这个模块就是专门用来负责反向解析别名的
    from django.urls import reverse
    #使用如下
    reverse('别名')
    #例如:
    return redirect(reverse('home'))     ----  翻译成  /home1/
    # 我们只需要在需要使用这个文件的时候将这个文件的别名使用reverse()方法反解析出来就行了
    
    或者直接可以写成 return redirect('home')    不需要使用serverse也可以解析出来
    
    # 在html文件中的引用
    <a href="{% url 'home' %} ">主页面</a>    ----  翻译成  /home1/
    #使用url对这个别名进行解析
    
    #带参数的反向解析:
    reverse('index',args=(10,11,))   -- /index2/10/
    
    #带参数的反向解析:
    {% url '别名' 参数1 参数2 %} 
    例如:{% url 'index' 10 %} -- /index2/10/  <a href='/index2/10/'>hhh</a>

Namespaces

Route distribution

  1. Because of the possibility to create multiple different app in the project, if these applications are written url app urls in the project file, if the file path too, will be a lot of files exist, modify them particularly troublesome, so we have to solve this problem

  2. How to solve this problem?

    #我们可以在每个应用下都写上一个urls.py文件,在这个文件中写入我们需要用到的url,
    
    # 但是浏览器找的url时会从你的项目文件urls文件去找,那么我们需要在项目的urls文件也配置一下
    # 首先导入一个包,专门负责路由分发的
    from django.conf.urls import include  
    
    #所以我们在项目下的urls文件中写url时这样写:
    urlpatterns = [
        url(r'^app01/',include('app01.urls')),
        url(r'^app02/',include('app02.urls')),
    ]
    # 这个意思就会只要匹配到访问路径是app01的就去app01.urls中找路径,匹配到访问路径是app02的就去app02.urls中找路径
  3. Write a route file urls.py under each app file

    1. app01 
        urlpatterns = [
            # url(r'^admin/', admin.site.urls),
            url(r'^home1/',views.home,name='home1'),
        ]
    
    2. app02 
        urlpatterns = [
            # url(r'^admin/', admin.site.urls),
            url(r'^home2/',views.home,name='home1'),
        ]
    
     # 当通过项目文件中的路由匹配到那个app下的时候就会在这个app路由中找相应的路由地址

Namespaces

  1. Because when we do the project will give url aliases, if there are multiple applications url alias of the same, then there will not find the file, an error

  2. So we need to urls in the project file to change the configuration

    urlpatterns = [
        url(r'^app01/',include('app01.urls',namespace='app01')),  
        url(r'^app02/',include('app02.urls',namespace='app02')),
    ]
    
    # namespace = '别名',意思是当前这个app01.urls中的url都归属于当前这个命名空间app01的
  3. When the application:

    #html文件中
    <a href="{% url 'app01:home' %}">返回首页</a>
    # 在别名的前面你需要加上这个命名空间的名称
    
    #在后端的文件中
    return redirect('app02:book2')   # 在别名的前面你需要加上这个命名空间的名称

Guess you like

Origin www.cnblogs.com/zhufanyu/p/11665512.html