7๐
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.
...
else:
# Return an 'invalid login' error message.
...
4๐
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')
- Using a custom form in a modelformset factory?
- No matching distribution found for django
- Django_auth_ldap no module named ldap
2๐
Try modifying:
profile_page = HttpResponseRedirect('profile')
to:
profile_page = HttpResponseRedirect(reverse('profile'))
- Send email confirmation after registration django
- Full outer join in django
- Handling HTTP chunked encoding with django
- Determine if Django is running under the development server
- Django + uwsgi + nginx + SSL
1๐
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())
else:
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)
- Django.core.paginator Ajax pagination with jQuery
- How to pass the remote IP to a proxied service? โ Nginx
- Django signal m2m_changed not triggered
- How do I check that user already authenticated from tastypie?
1๐
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
https://www.youtube.com/watch?v=aCotgGyS2gc&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK&index=35
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
- Passing a variable in redirect in Django
- Django: taking input and showing output in the same page
- How to pass the remote IP to a proxied service? โ Nginx
1๐
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.