简体   繁体   中英

Django 1.6: Redirect to last page after login not working

I want the user to be redirected to the last page after successful login whenever the user is prompted to login. Currently, it just redirects to the index page. I know this question has been asked several times and have also tried multiple ways of doing it but it doesnt work. I've also tried following this Django: Redirect to previous page after login and it doesn't work because most of the stuff is already deprecated in 1.6. So I've the following code now and it gives me this 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs. 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.

on line

129 : <h4><a class="writebtn"href=" {% url django.contrib.auth.views.login %} ?next={{request.path}}">Write Review</a></h4>

This is what i've so far

urls.py

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

login.html

<form method="post" action="{% url 'django.contrib.auth.views.login' %}"> {% csrf_token %}
    <input type="text" class="form-control" id="username" placeholder="Username" 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 }}" />

  </form>

I've this in my template to see if the user is authenticated to write a review - if not logged in it renders the login page but then it redirects to the index page

{% if user.is_authenticated and reviewed == False %}
      <h4><a class="writebtn"href="/usercontent/{{doctor.id}}/">Write Review</a></h4>

      {% elif user.is_authenticated and reviewed == True  %}
      <h4><a class="writebtn"href="">Already Reviewed!</a></h4>

      {% else %}
      <h4><a class="writebtn"href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Write Review</a></h4>

      {% endif %}

views.py

@login_required
def addContent(request, id):
    d = getVariables(request)
    doctor = Doctor.objects.get(id=id)
    params = {}
    params.update(csrf(request))

    if request.user.is_authenticated():
        user = request.user
        ds = DoctorSeeker.objects.get(user=user)
        d['doctorseeker'] = ds


    if request.method == "POST":
        form = UserContentForm(request.POST)

        if form.is_valid():
            time = form.cleaned_data['time']
            comment = form.cleaned_data['comment']

            if request.POST.get('Like') == 'Like':
                con = UserContent(time=time, comment = comment, liked = True, disliked = False, doctor_id = doctor.id, user_id = request.user.id)
                doctor.likes += 1
                doctor.netlikes = doctor.likes - doctor.dislikes
                doctor.save()
                con.save()

            elif request.POST.get('Like') == 'Dislike':
                con = UserContent(time=time, comment = comment, liked = False, disliked = True,  doctor_id = doctor.id, user_id = request.user.id)
                doctor.dislikes +=1
                doctor.netlikes = doctor.likes - doctor.dislikes
                doctor.save()
                con.save()

            url = '/docprofile/%s' % str(doctor.id)
            return HttpResponseRedirect(url)

    else:
        form = UserContentForm()

    d.update({'doctor': doctor, 'UGC': UserContent.objects.all(),
          'form': form })
    return render(request, 'meddy1/usercontent.html',d)

For the following template code to work:

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

The following must be in place:

  • You must have a reference to django.contrib.auth.views.login in your `urls.py:

     url(r'^login/$', 'django.contrib.auth.views.login') 
  • You must have a template that resolves to registration/login.html or you can specify a specific template path (which must exist) in the your url conf:

     url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': '<your template>'} ) 
  • In your settings.py , you must have:

     TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.request', ... ) 

    This automatically adds the request variable gets to your template context.

  • You must render your template with a RequestContext . There are many ways to do that, but the most simple is:

     from django.shortcuts import render def view(request): ... return render(request, '<template>', {<other context variables>}) 

    Using a RequestContext causes the django.core.context_processors.request context processor to run.

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