gpt4 book ai didi

python - Django - 查看,url 怪异

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:11 25 4
gpt4 key购买 nike

我注意到 Django 处理我的 url 模式的方式有一个奇怪的行为。用户应该登录,然后被重定向到他们的个人资料页面。我还可以让用户编辑他们的个人资料。

这是我的一个应用程序的 URL 模式:

urlpatterns=patterns('student.views',
(r'profile/$', login_required(profile,'student')),
(r'editprofile/$', login_required(editprofile,'student')),
)

这是一个名为 student 的应用程序。如果用户转到/student/profile 他们应该获得个人资料 View 。如果他们去/student/editprofile 他们应该得到 editprofile View 。我设置了一个名为 login_required 的函数,它对用户进行一些检查。这比我仅使用注释处理起来要复杂一些。

这是 login_required:

def login_required(view,user_type='common'):
print 'Going to '+str(view)
def new_view(request,*args,**kwargs):
if(user_type == 'common'):
perm = ''
else:
perm = user_type+'.is_'+user_type
if not request.user.is_authenticated():
messages.error(request,'You must be logged in. Please log in.')
return HttpResponseRedirect('/')
elif request.user.is_authenticated() and user_type != 'common' and not request.user.has_perm(perm):
messages.error(request,'You must be an '+user_type+' to visit this page. Please log in.')
return HttpResponseRedirect('/')
return view(request,*args,**kwargs)
return new_view

无论如何,奇怪的是,当我访问/student/profile 时,即使我到达了正确的页面,login_required 也会打印以下内容:

Going to <function profile at 0x03015DF0>
Going to <function editprofile at 0x03015BB0>

为什么要打印两者?为什么要同时访问两者?

更奇怪的是,当我尝试访问/student/editprofile 时,加载的是个人资料页面,这是打印的内容:

Going to <function profile at 0x02FCA370>
Going to <function editprofile at 0x02FCA3F0>
Going to <function view_profile at 0x02FCA4F0>

view_profile 是一个完全不同的应用程序中的函数。

最佳答案

这两种模式:

(r'profile/$', login_required(profile,'student')),
(r'editprofile/$', login_required(editprofile,'student')),

两者都匹配 http://your-site/student/editprofile

尝试:

(r'^profile/$', login_required(profile,'student')),
(r'^editprofile/$', login_required(editprofile,'student')),

Django 使用谁的模式首先匹配的 View ( see number 3 here )。

关于python - Django - 查看,url 怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4785605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com