简体   繁体   中英

login_required - login success not redirecting to “next”

I am googling for 2 hours now and no sign of success.

after successfull login, I need to redirect user to the page where he was before login, using next parameter in the url.

but if i do print request.GET.get('next') , i am getting None even if i have next in the url like ?next=/next-page/ why is this?

my login view.

    username = request.POST.get('username_login')
    pwd = request.POST.get('pwd_login')
    user = authenticate(username=_username, password=pwd)        
    if user is not None:
        if user.is_active:
            login(request, user)
            print request.GET.get('next')
            if request.GET.get('next', False):
                return HttpResponseRedirect(request.GET.get('next'))
            else:
                return render_to_response("profil.html",{},context_instance=RequestContext(request))
        else:#user not active
            fail = 'account inactive' 
            return render_to_response("anmelden.html",{'fail':fail},context_instance=RequestContext(request))

i am using django 1.4 and i am using contrib.auth.login() . why cannot i redirect to the page?

if you are using django.contrib.auth.login() that should mean you have simply to override django default login by using LOGIN_REDIRECT_URL for example, you only have to override the views.py if you want to sign up and redirect to login. so you can override django login urls.py login.html and other.

 settings.py:
 LOGIN_REDIRECT_URL='/' # to redirect to the home after login

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

login.html :
<div id="main">
{% if form.errors %}
     <ul class="errorlist">
     <li>You entered an invalid username or password</li>
    </ul>       <br/>
      {% endif %}
      <form method="POST" action="{% url "django.contrib.auth.views.login" %}">{% csrf_token %}
        <table summary="login" id="login">
        <caption>Login</caption>
        <tr>
                <td>{{form.username.label_tag}}:</td>
                <td>{{form.username}}</td>          
        </tr>

        <tr>
                <td>{{form.password.label_tag}}:</td>
                <td>{{form.password}}</td>          
        </tr>           
        <tr>
        <td colspan="2" class="right">
    <input type="submit" value="Login" /></td>          
        </tr>
        </table>
        <input type="hidden" name="next" value="{{next}}" />
        </form>

    </div>

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