A few of the options in the django settings file are urls, for example LOGIN_URL
and LOGIN_REDIRECT_URL
. Is it possible to avoid hardcoding these urls, and instead use reverse url mapping? At the moment this is really the only place where I find myself writing the same urls in multiple places.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
In django development version reverse_lazy() becomes an option: https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse-lazy
Django 1.5 and later
As of Django 1.5,
LOGIN_URL
andLOGIN_REDIRECT_URL
accept named URL patterns. That means you don't need to hardcode any urls in your settings.For Django 1.5 - 1.9, you can also use the view function name, but this is not recommended because it is deprecated in Django 1.8 and won't work in Django 1.10+.
Django 1.4
For Django 1.4, you can could use
reverse_lazy
Django 1.3 and earlier
This is the original answer, which worked before
reverse_lazy
was added to DjangoIn urls.py, import settings:
Then add the url pattern
Note that you need to slice
LOGIN_URL
to remove the leading forward slash.In the shell: