Reputation: 741
PYTHON:
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from django.contrib.auth import authenticate
class Firstapp(View):
def get(self, request):
return render(request, 'index.html')
def post(self, request):
username = request.POST['your_name']
user = authenticate(username=username)
return HttpResponse(user)
HTML:
<form method="POST">
{% csrf_token %}
<input type="text" name ='your_name' placeholder="username"/>
<button type="submit">Button</button>
I have created many users: admin, user_one, user_two etc. Checked via /admin page and usernames are there as they should be. Followed by documentantion in Django I wrote the code, however I don't understand why submiting a username which is in database returns none in Fistapp.post? like authnetication(username='user_one') returns None. Would appreciate your help.
Upvotes: 0
Views: 39
Reputation: 44
If you are using the default Django backend to authenticate, a password would be required
If what you needs is a authentication without password, I recommend you read this doc
Django would always returns None if has an invalid authentication
Upvotes: 1