请教各位高手Django的url配置问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请教各位高手Django的url配置问题相关的知识,希望对你有一定的参考价值。

参考技术A 流线型化(streamlinling)函数导入

1:直接导入视图函数
from django.conf.urls.defaults import *
from mysite.views import hello
urlpatterns = patterns('',
(r'^hello/$', hello),
(r'^time/$', time),
)
2:导入包函模块名和函数的客串,而不是函数本身
from django.conf.urls.defaults import *
from mysite import views
urlpatterns = patterns('',
(r'^hello/$', 'views.hello'),
(r'^time/$', 'views.time'),
)
3:不用导入任何视图内容,写清楚字符串描述
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^hello/$', 'mysite.views.hello'),
(r'^time/$', 'mysite.views.time'),
)
4:提取一个公共视图前缀
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.views',
(r'^hello/$', 'hello'),
(r'^time/$', 'time'),
)
5:partterns()返回对对象的相加
旧:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.view',
(r'^hello/$', 'hello'),
(r'^time/$', 'time'),
(r'^other/$', 'mysite.others.other'),
)
新:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.view',
(r'^hello/$', 'hello'),
(r'^time/$', 'time'),
)
urlpatterns += patterns('mysite.others',
(r'^other/$', 'other'),
)
6:到目前为止,在所有URLconf例子中,我们使用的很简单,即“无命名”正则表达式组,在我们
想要捕获的URL部分加上小括号,django会将捕获的文本作为位置参数传递给视图函数。在更
高级的用法中,还可以使用“命名”正则表达式组来捕获URL,并且将其作为“关键字”参数 传给视图。

在python正则表达式中,命名的正则表达式组的语法是(?p<name>pattern),这里name是组的名字,而pattern是匹配的某个模式。

下面是一个使用无名组的URLconf的例子:
from django.conf.urls.defaults import *
from mysite import views
urlpatterns = patterns('',
(r'^arcticles/(\d4)/$',views.year_archive),
(r'^arcticles/(\d4)/(\d2)/$', view.moth_archive),
)
下面是相同的URLconf,使用命名组进行了重写:
from django.conf.urls.defaults import *
from mysite import views
urlpatterns = patterns('',
(r'^arcticles/(?P<year>\d4)/$', veiws.year_archive),
(r'^arcticles/(?P<year>\d4)/(?P<month>\d2)/$', veiws.month_archive),
)
如果不带命名组,请求/arcticle/2006/03/将会等于这样的函数调用:
month_archive(request, '2006', '03')
而带命名组,同样的请求就是这样的函数调用:
month_archive(request, year='2006', month='03')
从中可以看出,使用命名组可以让你的URLconf更加清晰,减少参数次序可能搞混潜在的Bug,
还可以让你的定义中对参数重新排序。

7:视图冗余:
# urls.py

from django.conf.urls.defaults import *
from mysite import views

urlpatterns = patterns('',
(r'^(foo)/$', views.foobar_view),
(r'^(bar)/$', views.foobar_view),
)

# views.py

from django.shortcuts import render_to_response
from mysite.models import MyModel

def foobar_view(request, url):
m_list = MyModel.objects.filter(is_new=True)
if url == 'foo':
template_name = 'template1.html'
elif url == 'bar':
template_name = 'template2.html'
return render_to_response(template_name, 'm_list': m_list)
利用上面的方法可以减少视图冗余
8:为url里传入固定的参数:
# urls.py

from django.conf.urls.defaults import *
from mysite import views

urlpatterns = patterns('',
(r'^foo/$', views.foobar_view, 'template_name': 'template1.html'),
(r'^bar/$', views.foobar_view, 'template_name': 'template2.html'),
)

# views.py

from django.shortcuts import render_to_response
from mysite.models import MyModel

def foobar_view(request, template_name):
m_list = MyModel.objects.filter(is_new=True)
return render_to_response(template_name, 'm_list': m_list)
一些URL参数详解:
/mydata/jan/01/
/mydata/jan/02/
/mydata/jan/03/
以上为三个url我们可以用不同的方式实现
1: (r'^mydata/(/w3)/(/d/d)', views.mydata) mydata(request, 'jan', '01')
2: (r'^mydata/(?P<month>/w3)/(?P<day>/d/d)', view.mydata) mydata(request, month='jan', day='01')
3: (r'^mydata/birthday/$', views.mydata, month:'jan', day:'01') mydata(request, month='jan', day='01')
9:include导入:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^weblog/', include('mysite.blog.urls')),
(r'^photos/', include('mysite.photos.urls')),
(r'^about/$', 'mysite.views.about'),
)
10:实例:
验证用户是否登录:
以下是三个视图
def my_view1(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/accounts/login/')
# ...
return render_to_response('template1.html')

def my_view2(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/accounts/login/')
# ...
return render_to_response('template2.html')

def my_view3(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/accounts/login/')
# ...
return render_to_response('template3.html')
以下是对视图进行包装:
def requires_login(view):
def new_view(request, *args, **kwargs):
if not request.user.is_authenticated(): #如果用户没有登录,跳转到登录界面
return HttpResponseRedirect('/accounts/login/')
return view(request, *args, **kwargs) #否则返回传进来的方法
return new_view #返回new_view值:登录视图或者是传进来的视图
以下为URL配置:
from django.conf.urls.defaults import *
from mysite.views import requires_login, my_view1, my_view2, my_view3

urlpatterns = patterns('',
(r'^view1/$', requires_login(my_view1)),
(r'^view2/$', requires_login(my_view2)),
(r'^view3/$', requires_login(my_view3)),
)
参考技术B 流线型化(streamlinling)函数导入 1:直接导入视图函数 from django.conf.urls.defaults import * from mysite.views import hello urlpatterns = patterns('', (r'^hello/$', hello), (r'^time/$', time), ) 2:导入包函模块名函数客串函数本身 from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^hello/$', 'views.hello'), (r'^time/$', 'views.time'), ) 3:用导入任何视图内容写清楚字符串描述 from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^hello/$', 'mysite.views.hello'), (r'^time/$', 'mysite.views.time'), ) 4:提取公共视图前缀 from django.conf.urls.defaults import * urlpatterns = patterns('mysite.views', (r'^hello/$', 'hello'), (r'^time/$', 'time'), ) 5:partterns()返象相加 旧: from django.conf.urls.defaults import * urlpatterns = patterns('mysite.view', (r'^hello/$', 'hello'), (r'^time/$', 'time'), (r'^other/$', 'mysite.others.other'), ) 新: from django.conf.urls.defaults import * urlpatterns = patterns('mysite.view', (r'^hello/$', 'hello'), (r'^time/$', 'time'), ) urlpatterns += patterns('mysite.others', (r'^other/$', 'other'), ) 6:目前止所URLconf例我使用简单即命名则表达式组我 想要捕获URL部加括号django捕获文本作位置参数传递给视图函数更 高级用使用命名则表达式组捕获URL并且其作关键字参数 传给视图 python则表达式命名则表达式组语(?ppattern)name组名字pattern匹配某模式 面使用名组URLconf例: from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^arcticles/(\d4)/$',views.year_archive), (r'^arcticles/(\d4)/(\d2)/$', view.moth_archive), ) 面相同URLconf使用命名组进行重写: from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^arcticles/(?P\d4)/$', veiws.year_archive), (r'^arcticles/(?P\d4)/(?P\d2)/$', veiws.month_archive), ) 带命名组请求/arcticle/2006/03/等于函数调用: month_archive(request, '2006', '03') 带命名组同请求函数调用: month_archive(request, year='2006', month='03') 看使用命名组让URLconf更加清晰减少参数序能搞混潜Bug 让定义参数重新排序 7:视图冗余: # urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^(foo)/$', views.foobar_view), (r'^(bar)/$', views.foobar_view), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foobar_view(request, url): m_list = MyModel.objects.filter(is_new=True) if url == 'foo': template_name = 'template1.html' elif url == 'bar': template_name = 'template2.html' return render_to_response(template_name, 'm_list': m_list) 利用面减少视图冗余 8:url传入固定参数: # urls.py from django.conf.urls.defaults import * from mysite import views urlpatterns = patterns('', (r'^foo/$', views.foobar_view, 'template_name': 'template1.html'), (r'^bar/$', views.foobar_view, 'template_name': 'template2.html'), ) # views.py from django.shortcuts import render_to_response from mysite.models import MyModel def foobar_view(request, template_name): m_list = MyModel.objects.filter(is_new=True) return render_to_response(template_name, 'm_list': m_list) 些URL参数详解: /mydata/jan/01/ /mydata/jan/02/ /mydata/jan/03/ 三url我用同式实现 1: (r'^mydata/(/w3)/(/d/d)', views.mydata) mydata(request, 'jan', '01') 2: (r'^mydata/(?P/w3)/(?P/d/d)', view.mydata) mydata(request, month='jan', day='01') 3: (r'^mydata/birthday/$', views.mydata, month:'jan', day:'01') mydata(request, month='jan', day='01') 9:include导入: from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^weblog/', include('mysite.blog.urls')), (r'^photos/', include('mysite.photos.urls')), (r'^about/$', 'mysite.views.about'), ) 10:实例: 验证用户否登录: 三视图 def my_view1(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/accounts/login/') # ... return render_to_response('template1.html') def my_view2(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/accounts/login/') # ... return render_to_response('template2.html') def my_view3(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/accounts/login/') # ... return render_to_response('template3.html') 视图进行包装: def requires_login(view): def new_view(request, *args, **kwargs): if not request.user.is_authenticated(): #用户没登录跳转登录界面 return HttpResponseRedirect('/accounts/login/') return view(request, *args, **kwargs) #否则返传进 return new_view #返new_view值:登录视图或者传进视图 URL配置: from django.conf.urls.defaults import * from mysite.views import requires_login, my_view1, my_view2, my_view3 urlpatterns = patterns('', (r'^view1/$', requires_login(my_view1)), (r'^view2/$', requires_login(my_view2)), (r'^view3/$', requires_login(my_view3)),

请教各位高手 oracle 存储过程 如何获得 捕获异常的内容

rt

参考技术A --通过sqlcode , sqlerrm 这两个内置变量来查看,例如:

DECLARE
--声明异常
some_kinds_of_err EXCEPTION; -- Exception to indicate an error condition

v_ErrorCode NUMBER; -- Variable to hold the error message code
v_ErrorText VARCHAR2(200); -- Variable to hold the error message text

BEGIN
--...
--抛出异常
IF ( ... ) THEN --(括号内填抛出异常的条件)
RAISE some_kinds_of_err;
END IF;
--...
EXCEPTION
--捕捉异常
WHEN some_kinds_of_err THEN
/* do something to Handler the errors */
null;
--捕捉其他异常,并获得 捕获异常的内容
WHEN OTHERS THEN
v_ErrorCode := SQLCODE;
v_ErrorText := SUBSTR(SQLERRM, 1, 200);
-- Note the use of SUBSTR here.

dbms_output.put_line(v_ErrorCode || '::'||v_ErrorText);
END;
/

/**
sqlcode 就是错误代码
sqlerrm 就是sql错误信息。注意用substr来截取,否则输出很难看。

**/本回答被提问者采纳
参考技术B begin
plsql语句串;
exception when others
then
dbms_output.put_line(sqlcode||' '|| SQLERRM);

end;

以上是关于请教各位高手Django的url配置问题的主要内容,如果未能解决你的问题,请参考以下文章

客户端的SoftEther连接不上!!!请教各位SoftEther高手!

请教高手教我如何配置java编译器

客户端的SoftEther连接不上!!!请教各位SoftEther高手!

请教大神怎么配置tortoisegit和gitlab

ubuntu安装smaba服务中出错,提示如下图!请教各位高手!!谢谢!

为啥FlashFXP连接不了,请教各位高手啊!!!