[Django]-Django – view, url weirdness


These two patterns:

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

Both match http://your-site/student/editprofile.


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

Django uses the view who’s pattern matches first (see number 3 here).



Not sure why you can’t use the standard @login_required decorator – it seems that your version actually provides less functionality, given that it always redirects to \, rather than the actual login view.

In any case, the reason why both are printed is because the print statement is in the top level of the decorator, and thus is executed when the urlconf is evaluated. If you put it in the inner new_view function, it will only be executed when it is actually called, and should print only the relevant view name.


Your login_required looks like it’s a python decorator. Any reason you need to have it in your urls.py?

I think the print 'Going to '+str(view) line is getting evaluated when urlpatterns is read to determine which view to execute. It looks weird, but I don’t think it’ll hurt you.

The line print 'Going to '+str(view) will not be executed every time the view is hit, only when the url pattern is evaluated (I think). The code in new_view is the only code that will execute for certain as part of the view.

Leave a comment