0

I am conditionally using two different url patterns, mean on one URL I am conditionally checking usertype and then making URL pattern accordingly.Following is what that is in my urls.py

from django.conf.urls.defaults import *
from project import settings  
from accounts.models import UserProfile

profile=request.user.get_profile() 

urlpatterns=patterns('accounts.views',
        (r'^register/$','register',{'template_name':'accounts/     register.html'},'register'),
 )

try:
   profile.profile1
   urlpatterns+=patterns("profile1.views",
        (r'^dashboard/$','dashboard'),
   )
except UserProfile.DoesNotExist:
   urlpatterns+=patterns("profile2.views",
        (r'^dashboard/$','dashboard'),
   )

urlpatterns+=patterns('django.contrib.auth.views',
        (r'^login/$','login',{'template_name':'account/login.html'},'login'),
)

Now when I tried to get user profile by using request.user.get_profile then django says request is not defined. It is true but how can I get this profile defined till that place by using some import or there is some other better way to do such thing?

Hafiz
  • 4,187
  • 12
  • 58
  • 111

1 Answers1

2

No code in Python ever has automatic access to variables defined elsewhere. Names must always either be defined within the current module, or imported from elsewhere. This applies to the request as much as to any other Python variable.

However, even if you were able to get a request variable into urls.py, this still would not work. URLconfs are general to the whole server process, not specific to each request. There's simply no such thing as "the request" or "the user" at the point when the urls are evaluated.

The correct way to do this is to define a simple view for dashboard that just checks request.user and then dispatches to the correct function from there.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • +1 for such detail answer, how can I dispatch to current function? do you mean just calling two different functions and returning those functions response? – Hafiz Apr 08 '12 at 20:36