I created my own view for login. However if a user goes directly to /admin it brings them to the admin login page and doesn't use my custom view. How can I make it redirect to the login view used for everything not /admin?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
- UnicodeEncodeError when saving ImageField containi
You can redirect admin login url to the auth login view :
http://blog.montylounge.com/2009/07/5/customizing-django-admin-branding/ (web archive)
I'm trying to solve exactly this problem and I found the solution at this guys blog. Basically, override the admin template and use your own template. In short, just make a file called login.html in /path-to-project/templates/admin/ and it will replace the admin login page. You can copy the original (django/contrib/admin/templates/login.html) and modify a line or two. If you want to scrap the default login page entirely you can do something like this:
There it is. One line in one file. Django is amazing.
In your ROOT_URLCONF file (by default, it's urls.py in the project's root folder), is there a line like this:
If so, you'd want to replace include(admin.site.urls) with the custom view you created:
or if your app has its own urls.py, you could include it like this:
I had the same issue, tried to use the accepted answer, but has the same issue as pointed in the comment above. Then I've did something bit different, pasting here if this would be helpful to someone.
The idea is that if the user is login, and tried to access the admin, then he gets 404. Otherwise, it will force you to the normal login page (unless you are already logged in)
I found that the answer above does not respect the "next" query parameter correctly.
An easy way to solve this problem is to use a simple redirect. In your site's urls file, immediately before including the admin urls, put a line like this:
From http://djangosnippets.org/snippets/2127/—wrap the admin login page with
login_required
. For example, inurls.py
:You probably already have the middle two lines and maybe even the first line; adding that fourth line will cause anything that would have hit the
admin.site.login
function to redirect to yourLOGIN_URL
with the appropriatenext
parameter.