0

i made a login that should redirect to the previous page (with /?next=/) which seem to work, but when there is no next= it stays on the loginpage without redirecting to the index. Where do i make my mistake?

def login_user(request):
    login_form = LoginForm(request.POST or None)
    if request.POST and login_form.is_valid():
        user = login_form.login(request)
        if user:
            login(request, user)
            return HttpResponseRedirect(request.POST.get('next', reverse('index')))
            #return HttpResponseRedirect(reverse('index'))

    return render(request, 'login.html', {'login_form': login_form, 'next': request.GET.get('next', '') })

My urls.py (in my users app)

from django.conf.urls import patterns, url
from users import views
urlpatterns = patterns('',
    #Authentication urls
    url(r'^login/$', views.login_user, name='login_user'),
    url(r'^logout/$', views.logout, name='logout'),
    url(r'^register/$', views.register_user, name='register_user'),
)

my urls.py (in my characters app)

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
)

my template: (yes it isn't a beaty, bit old piece of work)

{% block content %}
<div>
    <form action="{% url 'login_user' %}" method="post" class="login">{% csrf_token %}
        <table>
        {% if login_form.non_field_errors %}
            <tr>
                <td><i style="font-size: small;">{{ login_form.non_field_errors|striptags }}</i></td>
            </tr>
        {% endif %}
        {% for field in login_form %}
            <tr>
                <td>{{ field }}</td>
            </tr>
        {% endfor %}
            <tr>
                <td style="text-align:right;" >
                    <a href="{% url 'register_user' %}">register</a>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="submit" value="Login" />
                </td>
            </tr>
        </table>

        <input type="hidden" name="next" value="{{ next }}">
    </form>
</div>
{% endblock %}
Jonas
  • 121,568
  • 97
  • 310
  • 388
Hans de Jong
  • 2,030
  • 6
  • 35
  • 55
  • 1
    Did you check that ``reverse('index')`` correctly resolves to your homepage? – sthzg Apr 22 '14 at 12:01
  • possible duplicate of [Django: Redirect to previous page after login](http://stackoverflow.com/questions/806835/django-redirect-to-previous-page-after-login) – rnevius Apr 22 '14 at 12:01
  • the #return HttpResponseRedirect(reverse('index')) works just fine, – Hans de Jong Apr 22 '14 at 12:02
  • is your login function working properly? – skzryzg Apr 22 '14 at 12:03
  • yes everything works jsut as it should, except the redirect when there is no ?next=somethuing/ in the url. when there is it redirects jsut fine as well – Hans de Jong Apr 22 '14 at 12:05
  • could you post your urls.py? – skzryzg Apr 22 '14 at 12:07
  • Could you post your template as well? – knbk Apr 22 '14 at 12:12
  • Might be stupid... but did you pdb that ``request.POST.get('next')`` does really fail? Because the default value is only used when the key itself does not exist, not if it's empty. – sthzg Apr 22 '14 at 12:15
  • if i use request.post.get('next') then my redirect link is: /login/None – Hans de Jong Apr 22 '14 at 12:24
  • 1
    Okay, then I guess this should be the reason. Could you try instead of using the ``reverse`` as second parameter in the get() function to split that into an if else stmt? ``if request.POST.get('next'): # redirect to next else: # redirecto to reverse('index')`` – sthzg Apr 22 '14 at 12:29
  • with the request.GET.get('next', '') in my render request, and the if request.POST// else it works thanks alot – Hans de Jong Apr 22 '14 at 12:34
  • You're welcome, the use of ``or`` in @knbk 's answer is more elegant than the if-else though. – sthzg Apr 22 '14 at 12:47

1 Answers1

1

The problem is that if you don't have a next parameter specified in the GET parameters, it will be an empty string, but it will still be defined. request.POST.get('next') will never fail, but simply return an empty string. You can do the following:

def login_user(request):
    login_form = LoginForm(request.POST or None)
    if request.POST and login_form.is_valid():
        user = login_form.login(request)
        if user:
            login(request, user)
            return HttpResponseRedirect(request.POST.get('next') or reverse('index'))
            #return HttpResponseRedirect(reverse('index'))

    return render(request, 'login.html', {'login_form': login_form, 'next': request.GET.get('next', '') })

This will return the value of next, or reverse('index') if that value is empty.

knbk
  • 52,111
  • 9
  • 124
  • 122