zoukankan      html  css  js  c++  java
  • django url 跳转

    node2:/exam/mysite/polls#cat urls.py
    from django.conf.urls import url,include
    
    from . import views
    
    urlpatterns = [
        url(r'^$', views.index, name='index'),
        # ex: /polls/5/
        url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
        # ex: /polls/5/results/
        url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
        # ex: /polls/5/vote/
        url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    
        url(r'^articles/2003/$', views.special_case_2003),
        url(r'^articles/([0-9]{4})/$', views.year_archive),
        url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
        url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
    ]
    
    
    总入口:
    
    node2:/exam/mysite/mysite#pwd
    /exam/mysite/mysite
    node2:/exam/mysite/mysite#cat urls.py
    """mysite URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/1.11/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.conf.urls import url, include
        2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
    """
    from django.conf.urls import url,include
    from django.contrib import admin
    
    urlpatterns = [
       url(r'^polls/', include('polls.urls', namespace='polls')),
       url(r'^books/', include('polls.urls', namespace='polls')),
       url(r'^admin/', admin.site.urls),
    ]
    
    
    
    node2:/exam/mysite#python manage.py runserver 0.0.0.0:8000
    Performing system checks...
    
    System check identified some issues:
    
    WARNINGS:
    ?: (urls.W005) URL namespace 'polls' isn't unique. You may not be able to reverse all URLs in this namespace
    
    System check identified 1 issue (0 silenced).
    August 18, 2018 - 16:45:12
    Django version 1.11, using settings 'mysite.settings'
    Starting development server at http://0.0.0.0:8000/
    Quit the server with CONTROL-C.
    
    
    
    http://192.168.137.3:8000/books/articles/2003/
    
    /books/articles/2003/
    
    1.若要从URL 中捕获一个值,只需要在他周围放置一堆圆括号:
    
    
    node2:/exam/mysite/polls#cat urls.py
    from django.conf.urls import url,include
    
    from . import views
    
    urlpatterns = [
        url(r'^$', views.index, name='index'),
        # ex: /polls/5/
        url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
        # ex: /polls/5/results/
        url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
        # ex: /polls/5/vote/
        url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
    
        url(r'^articles/(2003)/$', views.special_case_2003),
    	
    	
    def special_case_2003(req,aaa):
       print 'calling special_case_2003'
       print aaa
       print req
       print req.get_full_path();
       return HttpResponse(req.get_full_path()+'|'+aaa)
       
       
      2. 不需要添加一个前导的反斜杠,因为每个URL都有。 
      
      例如,应该是^articles而不是^/articles/
      
      3.每个正则表达式前面的'r'是可选的但是建议加上,他告诉Python这个字符串是"原始的" --字符串中
      
      任何字符都不应该被转移  
      
      
  • 相关阅读:
    光纤收发器的连接及收发器指示灯故障诊断
    Keepalive介绍及工作原理
    RabbitMQ消息队列集群配置-1
    python2 centos7 安装mysql-python库
    etcd服务的安装与配置 yum安装新版本
    ETCD集群安装配置及简单应用 老版本
    查看mysql 最大连接数,连接线程数
    TCP连接状态详解及TIME_WAIT过多的解决方法
    Linux 进程打开最大文件连接数Too many open files
    redis 连接数 修改
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349088.html
Copyright © 2011-2022 走看看