How to pass a parameter to login view in django while reversing from login url in settings.py?

StackOverflow https://stackoverflow.com/questions/20949766

  •  24-09-2022
  •  | 
  •  

質問

I'm having the following issue: I have an app that requires a parameter to be passed at login so the url structure is like this -> http://baseURL.com/paramter/

where "parameter" is a variable that is matched later on from a database.

The project is working just fine except when the following scenario occurs: http://baseURL.com/paramter/home

where "home" is a view that requires the user to be logged in by @login_required so the project checks the login_url in settings.py file and tries to go there and then I get the following error:

NoReverseMatch at /parameter/home/

Reverse for 'login_view' with arguments '()' and keyword arguments '{}' not found. 

In my settings.py

LOGIN_URL = reverse_lazy('login_view')

In my views.py

def login_view(request, parameter):

In urls.py

url(r'^(?P<parameter>\w+)/$', 'myproject.apps.myapp.views.login_view'),

So how to pass that parameter if settings.py goes to the login view before the url config?

役に立ちましたか?

解決

You can simply pass arguments to the reverse function as kwargs

LOGIN_URL = reverse_lazy('login_view', kwargs={'parameter': 'login_parameter'})

https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse

Usually the login url does not belong into settings.py. You just put it into your urls.py

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top