zoukankan      html  css  js  c++  java
  • python django url导入

    http://192.168.137.3:8000/articles/2005/
    
    
    Page not found (404)
    Request Method: 	GET
    Request URL: 	http://192.168.137.3:8000/articles/2005/
    
    Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
    
        ^admin/
        ^blog/$
    
    The current path, articles/2005/, didn't match any of these.
    
    You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
    
    
    
    默认是去找 mysite下的urls.py文件
    
    node2:/django/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
    from django.contrib import admin
    from blog import views as view
    urlpatterns =( 
    url(r'^admin/', admin.site.urls),
    url(r'^blog/$',view.archive),
    )
    
    
    
    没有去找我news app下的urls.py文件?
    
    node2:/django/mysite/news#cat urls.py 
    from django.conf.urls import url
    
    from . import views
    
    urlpatterns = [
        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),
    ]
    
    
    
    在Django框架中,提供了非常清晰简洁的url管理方法,在新建一个project之后(此处为myproject),
    
    然后在项目中建立一个app(此处为myapp),会看到有如下的目录结构:
    
    一般所熟知的就是在myproject/myproject/urls.py中的 urlpatterns列表中来配置url,
    
    node2:/django/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 include,url
    from django.contrib import admin
    from blog import views as view
    from news import *
    urlpatterns =( 
    url(r'^admin/', admin.site.urls),
    url(r'^blog/$',view.archive),
    url(r'^articles/',include(news.urls))
    )
    
    每一个列表项就是一个由url函数的调用,例如假定我们想在myapp中定义一个主页,
    
    然后通过"http://localhost:8000/myapp/homepage"来访问,首先我们在myproject/myapp//view.py中定义一个叫homePage的函数(名字随意,不一定叫这名字):
    
    
    但是假设一个project 中有多个app,用以上的方式来管理url可能会造成比较混乱的局面,
    
    
    为了解决这个问题,我们可以用include的方法来配置url,首先在我们自己的app中建立一个urls.py
    
    myproject/myapp/目录下建立urls.py,然后在其中输入如下内容:
    [python] view plain copy
    from django.conf.urls import url  
      
    from myapp.views import homePage  
      
      
    urlpatterns = [  
        url(r'homepage', homePage),  
    ]  
    
    
    from django.conf.urls import url, include#导入了include函数  
    from django.contrib import admin  
      
    urlpatterns = [  
        url(r'^admin/', admin.site.urls),  
        url(r'^myapp/', include("myapp.urls"))#包含myapp中的urls  
    ]  
    
    
    
    demo:
    
    node2:/django/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 include,url
    from django.contrib import admin
    from blog import views as view
    urlpatterns =( 
    url(r'^admin/', admin.site.urls),
    url(r'^blog/$',view.archive),
    url(r'^articles/',include("news.urls"))
    )
    
    node2:/django/mysite/news#cat urls.py
    from django.conf.urls import url
    
    from . import views
    
    urlpatterns = [
        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),
    ]
    

  • 相关阅读:
    HDU 5119 Happy Matt Friends(DP || 高斯消元)
    URAL 1698. Square Country 5(记忆化搜索)
    POJ 2546 Circular Area(两个圆相交的面积)
    URAL 1430. Crime and Punishment(数论)
    HDU 1111 Secret Code (DFS)
    HDU 1104 Remainder (BFS求最小步数 打印路径)
    URAL 1091. Tmutarakan Exams(容斥原理)
    PDO连接mysql8.0报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误
    swoole简易实时聊天
    centos安装netcat
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349420.html
Copyright © 2011-2022 走看看