私は、Djangoが私のURLパターンをどのように処理しているかに奇妙な振る舞いを感じました。ユーザーはログインして、プロフィールページにリダイレクトする必要があります。私はまた、ユーザーが自分のプロフィールを編集する能力を持っています。ここでDjango - view、url weird
は、私のアプリケーションのいずれかの私のURLパターンです:
urlpatterns=patterns('student.views',
(r'profile/$', login_required(profile,'student')),
(r'editprofile/$', login_required(editprofile,'student')),
)
これは、学生と呼ばれるアプリです。ユーザーが/ student/profileに行くと、プロフィールビューが表示されます。彼らが/ student/editprofileに行くと、editprofileビューが得られるはずです。私は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
とにかく、奇妙なことがlogin_required、私は右のページに行くにもかかわらず、私は/学生/プロフィールを訪問したときに、ということである次のように出力されます
Going to <function profile at 0x03015DF0>
Going to <function editprofile at 0x03015BB0>
なぜ両方を印刷していますか?なぜそれは両方を訪問しようとしていますか?私は/学生/ editprofileを訪問しようとすると、
でも奇妙には、プロフィールページには、どのような負荷であり、これは印刷されているものです:
Going to <function profile at 0x02FCA370>
Going to <function editprofile at 0x02FCA3F0>
Going to <function view_profile at 0x02FCA4F0>
view_profileは完全に別のアプリで機能です。
decorators.login_required()とdecorators.permission_required()については何がそんなに耐えられないのですか? – hop