我正在使用 django 創建用戶注冊。我用我的視圖寄存器創建了一個簡單的 HTML 文件。但是當我點擊提交時,它給了我一個錯誤:email_name, domain_part = email.strip().split('@', 1)AttributeError: 'tuple' object has no attribute 'strip'我的HTML:<form action="register" method="post">{% csrf_token %}<input type="text" name="first_name" placeholder="Enter ur nem"><br><input type="text" name="last_name" placeholder="Enter ur surname"><br><input type="email" name="email" placeholder="Enter ur email"><br><input type="text" name="username" placeholder="Enter ur Usetrname"><br><input type="password" name="password1" placeholder="Enter ur password"><br><input type="password" name="password2" placeholder="Enter again your password"><br><input type="submit"> </div>我的看法:from django.contrib.auth.models import User , authdef register(request):if request.method == 'POST': first_name= request.POST['first_name'], last_name= request.POST['last_name'], email= request.POST['email'], password1 = request.POST['password1'], password2= request.POST['password2'], username= request.POST['username'], if password1 == password2: if User.objects.filter(username=username).exists(): print('usernem taken') else: myuser= User.objects.create_user(username=username, password = password1, email= email, first_name = first_name, last_name= last_name) myuser.save(); print ('user saved') else: print('passwords do not match') return redirect ('/')
1 回答

莫回無
TA貢獻1865條經驗 獲得超7個贊
first_name= request.POST['first_name'],
通過像逗號這樣的方式結束行,first_name
這不是您所期望的字符串;事實上,它是一個只有一個元素的元組。如果您執行以下操作,您可能會看到這一點:
>>> t = "test_string", >>> t ('test_string',)
strip
然后,當您嘗試調用元組而不是字符串時,您會收到錯誤。
要解決此問題,您需要刪除從 中提取值的所有行上的尾隨逗號request.POST
,因此
email= request.POST['email'],
變成
email = request.POST['email']
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報
0/150
提交
取消