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>
我创建了很多用户:admin,user_one,user_two等。通过/ admin页面检查,用户名就在那里。接下来是Django中的documentantion我编写了代码,但是我不明白为什么提交数据库中的用户名会在Fistapp.post中返回none?像authnetication(username ='user_one')返回None。非常感谢您的帮助。
答案 0 :(得分:1)