1

I want my user to be redirected to the last page after successfully logging in. But the user is redirected to the index page

this is what I've in my header

  <li><a href="/login">Login</a></li>

here is the login form

<form method="post" action="{% url 'django.contrib.auth.views.login'  %}" >
        {% if form.errors %}
          <p>Your username and password don't match our records. Please try again.</p>
        {% endif %}
        <div class="login">
       <h1> LOG IN </h1>
          <input type="text" class="form-control" id="username" placeholder="Email" name="username">
         <input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">

          <button class="btn btn-primary" type="submit" name="submit" id="ss-submit">LOG IN</button>
          <input type="hidden" name="next" value="{{ next }}" />
          <!-- <a href="/forgotpassword">Forgot your password?</a> -->

           <p>Don't have an account? <a href="/signup">Sign up</a></p>

        </div>
        <!-- End Log In -->
          {% csrf_token %}
      </form>

Here is the login

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

here is a template

def dentists(request):
    d = getVariables(request)
    doctors = Doctor.objects.all().order_by('-likes')
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')
    try:
        doctors = paginator.page(page)
    except PageNotAnInteger:
        doctors = paginator.page(1)
    except EmptyPage:
        doctors = paginator.page(paginator.num_pages)
    d['doctors'] = doctors
    d['paginator'] = paginator
    return render_to_response('meddy1/dentists.html',d)
James L.
  • 1,143
  • 22
  • 52

1 Answers1

0

You dont need to make a view for login; just an url in urls.py

from django.conf.urls import patterns

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

And your template could be:

{% extends "base.html" %}
{% load i18n %}

{% block content %}
{% if form.errors %}
    <p style="color:red;">username or passsword is wrong. Try again</p>
{% endif %}
<div class="login">
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
    <table class="login">
        {{ form }}
    </table>
    <br><br><br>
    Login
    <input type="submit" value="Login"/>
    <input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
{% endblock %}

In your views if they are function views:

from django.http import HttpResponse, HttpResponseRedirect

def my_view(request):
    if request.user.is_authenticated():
        # do view stuff returning some HttpResponse
    else:
        return HttpResponseRedirect('/login/?next=%s' % request.path)

Or more simple, using @login_required:

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    # do view stuff returning some HttpResponse

If your view is a CBV, put login_required in urls.py like this:

from django.contrib.auth.decorators import login_required

url(r'^app/url/$', login_required(MyView.as_view()), name='my-view'),

That redirects to the page before the user log in.

elmonkeylp
  • 2,674
  • 1
  • 16
  • 14
  • I get Invalid block tag: 'trans' error. I've also tried adding the input type hidden field in my existing login template and it still redirects me to the index page. – James L. Jun 16 '14 at 03:46
  • What about your view? That what you call when your are logged out. You use this? return HttpResponseRedirect('/app/login/?next=%s' % request.path) – elmonkeylp Jun 16 '14 at 15:09
  • I edited my Answer just now, to be more especific about the posible solution to your problem. Sorry about the trans tags, avoid them, I used that from an a third party app. – elmonkeylp Jun 16 '14 at 15:31
  • The view function is not being called. I'm using the built in django login to authenticate. I've updated my answer above with the urls.py – James L. Jun 16 '14 at 16:00
  • I mean the others urls/views, thay you call when you are logged-out and you wish to be redirected after login. return HttpResponseRedirect('/login/?next=%s' % request.path) – elmonkeylp Jun 16 '14 at 16:14
  • I'm not sure if i follow you. So let's say I'm on one my templates and browsing some doctor objects, and I click login in the header. After I've logged in and I'm not redirected to the last page I was viewing. I've added a view for one of my templates above, where should I add your line of code to make it work ? – James L. Jun 16 '14 at 16:29
  • Change you link in your template to Login – elmonkeylp Jun 16 '14 at 17:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55723/discussion-between-haris-aghadi-and-elmonkeylp). – James L. Jun 16 '14 at 18:02