Django urls配置

浏览器向web服务器请求url地址时,urls.py文件中的patterns方法进行响应,响应的方法是通过url方法进行匹配的,匹配的过程中需要传入4个参数,4个参数的作用如下:

url(路径,视图,其它,name)

路径:使用regex的方式进行路径字符串匹配的

视图:是通过app下的views中的函数定义的

其它:可以传递任意参数到目标view

name:可以将此处的参数传递给特殊的templates中(templates后面再将,它是Django特有的编程模式——MVT,MVT编程模型中需要使用的,templates是专门负责web前端的显示)

e.g:
#app/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns(‘‘,
    # Examples:
    # url(r‘^$‘, ‘models_foreign.views.home‘, name=‘home‘),
    # url(r‘^blog/‘, include(‘blog.urls‘)),

    url(r‘^admin/‘, include(admin.site.urls)),
    url(r‘^$‘, views.index,name=‘index‘),
)

官方引用:

The url() function is passed four arguments, two required: regex and view, and two optional: kwargs, and name. At this point, it’s worth reviewing what these arguments are for.

url() argument: regex

The term “regex” is a commonly used short form meaning “regular expression”, which is a syntax for matching patterns in strings, or in this case, url patterns. Django starts at the first regular expression and makes its way down the list, comparing the requested URL against each regular expression until it finds one that matches.

Note that these regular expressions do not search GET and POST parameters, or the domain name. For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/. In a request to https://www.example.com/myapp/?page=3, the URLconf will also look for myapp/.

If you need help with regular expressions, see Wikipedia’s entry and the documentation of the re module. Also, the O’Reilly book “Mastering Regular Expressions” by Jeffrey Friedl is fantastic. In practice, however, you don’t need to be an expert on regular expressions, as you really only need to know how to capture simple patterns. In fact, complex regexes can have poor lookup performance, so you probably shouldn’t rely on the full power of regexes.

Finally, a performance note: these regular expressions are compiled the first time the URLconf module is loaded. They’re super fast (as long as the lookups aren’t too complex as noted above).

url() argument: view

When Django finds a regular expression match, Django calls the specified view function, with an HttpRequest object as the first argument and any “captured” values from the regular expression as other arguments. If the regex uses simple captures, values are passed as positional arguments; if it uses named captures, values are passed as keyword arguments. We’ll give an example of this in a bit.

url() argument: kwargs

Arbitrary keyword arguments can be passed in a dictionary to the target view. We aren’t going to use this feature of Django in the tutorial.

url() argument: name

Naming your URL lets you refer to it unambiguously from elsewhere in Django especially templates. This powerful feature allows you to make global changes to the url patterns of your project while only touching a single file.

When you’re comfortable with the basic request and response flow, read part 2 of this tutorial to start working with the database.

时间: 2024-11-04 01:25:30

Django urls配置的相关文章

Django urls配置方式

例子以上一篇随笔中的blog应用进行. urls.py默认生成的格式如下: 1 """whsweb URL Configuration 2 3 The `urlpatterns` list routes URLs to views. For more information please see: 4 https://docs.djangoproject.com/en/1.8/topics/http/urls/ 5 Examples: 6 Function views 7 1

Django及其配置(Mysql)

MVC和MTV模式 著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V):他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象和数据库的对象(ORM),视图负责与用户交互(页面),控制器(C)接收用户的输入调用模型和视图完成用户的请求. Django的MTV模式本质上与MVC模式没什么差别,也是各组件之间为了保持松耦合的关系,只是定义上有些许不同,Django的MTV分别代表: Mode(模型):负责业务对象与数据库的对象(ORM) Template

Django路由配置

Django路由配置系统.视图函数 1.路由配置系统(URLconf) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个URL调用这段代码,对于那个URL调用那段代码. urlpatterns = [ url(正则表达式, views视图函数,参数,别名), ] 参数说明: 一个正则表达式字符串 一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串 可选的要传递给视图函

django admin配置以及使用

django  admin配置以及使用 知识预览 admin组件使用 admin源码解析 admin组件使用 Django 提供了基于 web 的管理工具(django 2.0+, python3.6+). Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它: # Application definition# 新建一个应用名为mysite,注册application INSTALLED_APP

win环境下,django+postgresql配置

先下载postgresql的python包,选择合适自己的python版本和平台32位还是64位,http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下载win环境下postgresql接口,http://www.stickpeople.com/projects/python/win-psycopg/ 设置django settings.py DATABASES项 注:第2步如果不操作就会出现from psycopg2._psycopg import

Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of include()

Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of include() 报错信息 "F:\PyCharm 2016.2.2\bin\runnerw.exe" C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe F:/Django/blogs/manage.py run

Django+MySQLDB配置

Django+MySQLDB配置   来源: ChinaUnix博客 日期: 2009.07.09 16:25 (共有条评论) 我要评论                   一.安装Mysql(1)下载mysql-5.0.83(2)运行如下命令shell> groupadd mysqlshell> useradd -g mysql mysqlshell> gunzip  cd mysql-VERSIONshell> ./configure  --enable-thread-safe

Django URLs error: view must be a callable or a list/tuple in the case of include()

我现在的Django version 是 1.11.2, the folling solution is come from stackoverflow:==================================================================-----question: After upgrading to Django 1.10,I get the error: TypeError: view must be a callable or a list

Python Django Apache配置

项目结构目录: Apache 安装配置目录: C:\Apache2.2\conf\httpd.conf LoadModule wsgi_module modules/mod_wsgi.soWSGIScriptAlias / D:\TestApp\MyDjangoSite\mysite\mysite\wsgi.py<FilesMatch "D:/TestApp/MyDjangoSite/mysite"> Order allow,deny Deny from all Satis