简体   繁体   中英

Django Not Redirecting to Admin Site after Login

I needed one more login form field to login django admin. I overridden AdminSite.login_form

CODE:forms.py

from django.contrib.admin.forms import AdminAuthenticationForm
from django import forms
from django.contrib.admin.sites import AdminSite

    class ModifiedForm(AdminAuthenticationForm):
        auth_fact = forms.CharField(max_length=64,required=False)

    AdminSite.login_form = ModifiedForm

Then I rendered this from, CODE: views.py

def login_test(request):
if request.method == "POST":
    form = ModifiedForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        auth_fact= form.cleaned_data['auth_fact']
return render_to_response('admin/login.html',{'form':ModifiedForm}

And I created a template as well in templates/admin/login.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<p>Login Page</p>

    <div class="row-fluid">
        <div class="span12">

    <form action="" method="POST">{% csrf_token %}
        <table>
        {{ form.as_table }}
        </table>
<p style="margin-left:7%;"><small style="color: red;">example:product name,mfg date,exp date,batch no.</small></p>
<input style="margin-left:10%" class="btn btn-primary" type="submit" name="release" value="Update Product Details "/>
    </form>

    </div>
    </div>
</body>
</html>

All is working fine, I can submit login form. But I redirects me to the page:

http://example.com/accounts/profile/

And I am getting page not found error,but if put http://excample.com/admin in URL it shows me logged in and I can see admin site.

Basically I am able to login but its not redirecting me to admin page.

Any fix to this....

Set settings.LOGIN_REDIRECT_URL to /admin/ :

Default: '/accounts/profile/'

The URL where requests are redirected after login when the contrib.auth.login view gets no next parameter.

Or you can send next parameter to login view .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM