[Solved]-HttpResponseRedirect after login form not redirecting to profile


Changed in Django 1.10:
In older versions, when youโ€™re manually logging a user in, you must successfully authenticate the user with authenticate() before you call login(). Now you can set the backend using the new backend argument.

If you using Django<=1.10, you must use authenticate method before you login. Otherwise, you have to feed authentication backend at least in login method.
Here is the code snippet from django docs.

username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
    login(request, user)
    # Redirect to a success page.
    # Return an 'invalid login' error message.


try this

from django.shorcuts import redirect
from django.contrib.auth import authenticate
def login_page(request):
  profile_page = HttpResponseRedirect('profile')
  if request.user.is_authenticated():
    return profile_page
  form = LoginForm(request.POST or None)
  if request.POST and form.is_valid():
    user = authenticate(request,username=form.cleaned_data['username'],password=form.cleaned_data['password'])

    if user:
      login(request, user)
      return redirect('profile')


Try modifying:

  profile_page = HttpResponseRedirect('profile')


  profile_page = HttpResponseRedirect(reverse('profile'))


try with class bassed views

class Login(FormView, View):

    template_name = 'login/login.html'
    form_class = AuthenticationForm
    success_url = reverse_lazy("your_succes:url")

    def dispatch(self, request, *args, **kwargs):

        if request.user.is_authenticated():
            return HttpResponseRedirect(self.get_success_url())
            return super(Login, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        login(self.request, form.get_user())
        return super(Login, self).form_valid(form)


to get your page redirected to some other URL you actually require to use import redirect from Django shortcuts and utilize this to redirect to the required URL this is the ones available(as you already have created a Django website) in urls.py you can Have a look at the youtube video link


Another thing You may want to try is using templates as it makes Web developerโ€™s life Easier and safer

Answering the Question about gunicorn/nginx the application server has its default route set, you might have done it in the production build, to the profile page when the session info is added.

Also, check with the naming of the page profile it presently does not have any file extensions

you can also try with URL reverse

๐Ÿ‘คAloy A Sen


Instead of HttpResponseRedirect which triggers a HTTP 302, use a HttpResponseTemporaryRedirect to trigger a HTTP 307.

What happens is that 302 does not ensure the replay of the POST request. The reason is as follows:

RFC 1945 and RFC 2068 specify that the client is not allowed to change
the method on the redirected request. However, most existing user
agent implementations treat 302 as if it were a 303 response,
performing a GET on the Location field-value regardless of the
original request method. The status codes 303 and 307 have been added
for servers that wish to make unambiguously clear which kind of
reaction is expected of the client.

What's the difference between a 302 and a 307 redirect?


Leave a comment