无法使用Django返回User对象

时间:2017-02-02 03:32:23

标签: python django

我一直在尝试使用Django的身份验证系统登录用户。但我无法弄清楚如何创建一个@IBAction func switchKeyboards(_ sender: Any) { if isNumbers { txtField.keyboardType = UIKeyboardType.alphabet isNumbers = false } else { txtField.keyboardType = UIKeyboardType.numberPad isNumbers = true } txtField.resignFirstResponder() txtField.becomeFirstResponder() } 对象,到目前为止我一直在玩它:

User

程序不断返回def sign_in(request): form = NameForm(request.POST) if form.is_valid(): post = form.save() post.save() username = request.POST.get(post.question_text) password = request.POST.get(post.id_text) user = authenticate(username=username, password=password) if user is not None: login(request,user) return HttpResponse('hi') else: return HttpResponse('bye') else: form = NameForm() return render(request, 'checkin/sign_in_new.html', {'form': form}) 。我不确定我需要把它作为bye的参数,Django文档分别使用'username'和'password'。我是否需要先编写代码来创建用户?我在Django API中创建了一个

2 个答案:

答案 0 :(得分:1)

用户不存在。转到管理员并首先创建一个。是的,在您尝试登录之前需要创建用户。

答案 1 :(得分:0)

好的,我想出来了,我只需要先在代码中创建user。我刚刚添加user = User.objects.create_user('charlie', 'charlesdsmith25@gmail.com', 'smith')所以现在就是

def sign_in(request):
    #we need to handle all the data that was just typed, we'll add a condition for that
    form = NameForm(request.POST)
    if form.is_valid():
        post = form.save()
        post.save()
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = User.objects.create_user('charlie', 'charlesdsmith25@gmail.com', 'smith')
        auth = authenticate(username=username, password=password)
        if user is not None:
            login(request,user)
            return HttpResponse('hi')
        else:
            return HttpResponse('bye')
    else:
        form = NameForm()
    return render(request, 'checkin/sign_in_new.html', {'form': form})