2

With this code: urls.py

               url(r'^login/',
                   "django.contrib.auth.views.login"),

and tamplate like that:

<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>

which is redirecting to page with this code:

      {% if not user.is_authenticated %}
      <form method="post" action="">{% csrf_token %}
          {{ form.as_p }}
        <input type="submit" value="Zaloguj" />
        <input type="hidden" name="next" value="" />
      </form>
      {% else %}
      <h1>Już jestes zalogowany chujku</h1>
      {% endif %}

I have context processor 'django.core.context_processors.request' but after login in I am redirected to

accounts/profile/

When I am on login page url is: /accounts/login/?next=/gallery/newest/. More, after login in it loged me in but I am still in /accounts/login/?next=/gallery/newest/1/. Have I bugs in this code or answer is laying somewhere else?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
krzyhub
  • 6,285
  • 11
  • 42
  • 68
  • http://stackoverflow.com/questions/806835/django-redirect-to-previous-page-after-login – domino Jul 06 '11 at 13:03
  • I saw this and do waht was in top rated answer, but something is not right. – krzyhub Jul 06 '11 at 13:05
  • Are you getting redirected to `accounts/profile/` when you manually type-in the login url or when you click on the login link? – domino Jul 06 '11 at 13:45

2 Answers2

0

Just remove <input type="hidden" name="next" value="" /> from your form, it makes django think that next variable is empty.

dragoon
  • 5,601
  • 5
  • 37
  • 55
  • 1
    You're passing two values for `next`: one via GET and one via POST. Apparently, Django takes POST as predominant. – Chris Pratt Jul 06 '11 at 14:25
0

Finally i found answer. I think that this code did not use my own login.html template (but if i am wrong, please correct me):

url(r'^login/',
   "django.contrib.auth.views.login")

I change it to:

               url(r'^login/',
                   "django.contrib.auth.views.login", {'template_name': 'login.html'}),

Then I change my login.html tempalate code to:

  <form method="post" action="">{% csrf_token %}
      {{ form.as_p }}
    <input type="submit" value="Zaloguj" /> 
    <input type="hidden" name="next" value="{{ next }}" />

  </form>

I don't have the deeper meaning of what happend but this code which I copied here is working. Thanks for all for suggestions. It helps me to find answer and give a little better understanding of some things.

krzyhub
  • 6,285
  • 11
  • 42
  • 68