编辑这个页面须要登录或更高权限!
现在,我们有一个工作视图在前面的章节中解释。我们希望通过一个URL来访问该视图。 Django有他自己 URL映射的方式,现在我们来编辑项目中的 url.py 文件(myproject/url.py)完成。url.py 文件内容看起来如下:
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', #Examples #url(r'^, 'myproject.view.home', name = 'home'), #url(r'^blog/', include('blog.urls')), url(r'^admin', include(admin.site.urls)), ) , 'myproject.view.home', name = 'home'), #url(r'^blog/', include('blog.urls')), url(r'^admin', include(admin.site.urls)), )
当用户在您的网络应用程序发起一个网页的请求,Django控制器会接管通过url.py文件来寻找相应的视图,然后返回HTML响应或如果没有找到则返回404未找到错误。最重要的是“urlpatterns” 元组。这就是定义URL和视图之间的映射。一个映射的URL模式就像一个元组 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', #Examples #url(r'^, 'myproject.view.home', name = 'home'), #url(r'^blog/', include('blog.urls')), url(r'^admin', include(admin.site.urls)), url(r'^hello/', 'myapp.views.hello', name = 'hello'), ) , 'myproject.view.home', name = 'home'), #url(r'^blog/', include('blog.urls')), url(r'^admin', include(admin.site.urls)), url(r'^hello/', 'myapp.views.hello', name = 'hello'), )
标记行映射URL "/home" 到 myapp/ view.py文件中创建的Hello视图。正如你看到上面的映射是由三个要素 -
模式− 一个正则表达式匹配以URL予以解决和映射。 能够与python “re”模块工作一切都符合的模式(非常有用在你想通过URL来传递参数时)。 python到视图的路径−在你导入模块时相同。 名称−为了执行URL反转,需要使用URL模式的命名来执行上面示例。这样做以后,启动服务器,通过以下URL访问您的视图:http://127.0.0.1/hello
到目前为止,我们已经创建了“myprojects/url.py”文件的URL,然而,正如前面关于Django说明,创建一个应用程序,最好是能够重复使用在不同的项目中应用。 这样就可以很容易地看到的问题是什么,如果要保存所有网址中在“projecturl.py”文件中。 所以,最好的做法是创建每个应用程序的“url.py”,并把它列入我们的主项目 url.py 文件(包括在我们的管理界面管理的 URL 之前)。
我们需要使用下面的代码来创建一个 urls.py 文件在 myapp 文件夹中(myapp/urls.py) −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
myproject/urls.py 将变更为以下-
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', #Examples #url(r'^, 'myproject.view.home', name = 'home'), #url(r'^blog/', include('blog.urls')), url(r'^admin', include(admin.site.urls)), url(r'^myapp/', include(myapp.urls)), ) , 'myproject.view.home', name = 'home'), #url(r'^blog/', include('blog.urls')), url(r'^admin', include(admin.site.urls)), url(r'^myapp/', include(myapp.urls)), )
我们已经包括了 myapp 应用程序的所有URL。这是通过访问 home.html 为 “/hello”,现在是 “/myapp/hello” ,这是 Web应用程序的更好和更容易理解的结构。
再创建一个视图文件:C:\myproject\templates\myapp\hello.html,其内容如下:
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Hello </title> <meta name="robots" content="NONE,NOARCHIVE"> </style> </head> <body> <h2>Welcome to Nhooo .</h2> <p>This is my first Django App</p> <body> </html>
现在让我们想象一下在 myapp 的 “morning” 的另一个视图,我们希望它在映射myapp/url.py,we will then change our myapp/url.py to −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'), url(r'^morning/', 'myapp.views.morning', name = 'morning'), )
这可以被重构为 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url urlpatterns = patterns('myapp.views', url(r'^hello/', 'hello', name = 'hello'), url(r'^morning/', 'morning', name = 'morning'),)
正如你所看到的,我们现在使用 urlpatterns 元组的第一个元素。当你想改变应用程序的名称时,这非常有用。
现在我们知道如何映射URL,如何组织他们,现在就让我们来看看如何将参数传递给视图。一个经典的文章实例(您要访问文章到“/articles/article_id”)。
传递参数是通过URL模式正则表达式捕捉它们完成的。如果我们有类似以下的视图 “myapp/view.py”
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.shortcuts import render from django.http import HttpResponse def hello(request): return render(request, "hello.html", {}) def viewArticle(request, articleId): text = "Displaying article Number : %s"%articleId return HttpResponse(text)
我们想把它映射放在myapp/urls.py ,所以我们可以通过 “/myapp/article/articleId” 访问, 我们需要在下列 “myapp/urls.py” 定义−
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import url from . import views urlpatterns = [ url(r'^hello', views.hello, name='hello'), url(r'^article/(\d+)/', views.viewArticle, name = 'article'), url(r'^, views.index, name='index'), ] , views.index, name='index'), ]
当Django会看到URL: “/myapp/article/42” ,它将传递参数'42'到viewArticle视图,在浏览器中,应该得到以下结果 -
注意,参数的顺序这里是很重要的。假设我们希望查看一年中某个一个月的文章列表,现在添加一个 viewArticles 视图。 view.py 代码变为 -
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.shortcuts import render from django.http import HttpResponse def hello(request): return render(request, "hello.html", {}) def viewArticle(request, articleId): text = "Displaying article Number : %s"%articleId return HttpResponse(text) def viewArticles(request, month, year): text = "Displaying articles of : %s/%s"%(year, month) return HttpResponse(text)
相应的 myapp/urls.py 文件看起来如下 −
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url urlpatterns = patterns('myapp.views', url(r'^hello/', 'hello', name = 'hello'), url(r'^morning/', 'morning', name = 'morning'), url(r'^article/(\d+)/', 'viewArticle', name = 'article'), url(r'^articles/(\d{2})/(\d{4})', 'viewArticles', name = 'articles'),)
现在,当您访问 http://localhost:8000/myapp/articles/12/2015/,会得到'Displaying articles of: 2015/12' ,但如果转换参数,将不能得到相同的结果。
为避免这种情况,有可能一个URL参数会链接到视图参数。因此 url.py 将成为 -
# Filename : example.py # Copyright : 2020 By Nhooo # Author by : www.cainiaojc.com # Date : 2020-08-08 from django.conf.urls import patterns, include, url urlpatterns = patterns('myapp.views', url(r'^hello/', 'hello', name = 'hello'), url(r'^morning/', 'morning', name = 'morning'), url(r'^article/(\d+)/', 'viewArticle', name = 'article'), url(r'^articles/(?P\d{2})/(?P\d{4})', 'viewArticles', name = 'articles'),)