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.
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)