我正在使用最新版本的django和python 3,当我登录时,我收到以下错误消息.
django login()需要1个位置参数,但是给出了2个
请在下面找到我的登录视图的代码.
from django.shortcuts import render, get_object_or_404,redirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from authentication.forms import LoginForm, ForgottenPasswordForm, ResetPasswordForm
from authentication.functions import send_user_reset_password_link, resend_password_reset_link
from authentication.models import ResetPassword
# Create your views here.
def login(request):
error_message = None
heading = 'Login Form'
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
remember_me = form.cleaned_data['remember_me']
user = authenticate(request,username=username, password=password)
if not request.POST.get('remember_me', None):
#request.session.set_expiry(0)
if user is not None: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 django 创建注册表单,当我提交表单时,is valid() 函数失败,我不知道为什么。我的注册和登录页面都在一个 html 页面上,尽管我将所有字段名称称为不同的名称,例如 login_username。
表格.py
class SignUpForm(forms.Form):
username = forms.CharField(label='Username', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter Username'}))
conUsername = forms.CharField(label='Confirm Username', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Confirm Username'}))
firstName = forms.CharField(label='First Name', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter Firstname'}))
lastName = forms.CharField(label='Last Name', max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter LastName'}))
email = forms.CharField(label='Email', max_length=220,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter Email','type':'email'}))
conEmail = forms.CharField(label='Confirm Email', max_length=220,widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'Confirm Email','type':'email'}))
password = forms.CharField(label="Confirm Password", max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','type':'password','placeholder':'Enter Password'}))
conPassword = forms.CharField(label="Confirm Password", max_length=20,widget=forms.TextInput(attrs={'class': 'form-control','type':'password','placeholder':'Confirm Password'}))
Run Code Online (Sandbox Code Playgroud)
视图.py
def SignUp(request):
regForm = SignUpForm()
form = LoginForm()
if request.method == 'POST':
if regForm.is_valid(): …Run Code Online (Sandbox Code Playgroud) 我正在制作一个购物网站,我正在尝试使用产品信息初始化我的产品更新表单,但我无法将模型中的信息获取到表单中。
模型函数
def get_product_details(product_id):
product_details = Products.objects.filter(name=rproduct_id).select_related('name', 'description','price','qty')
return product_details
Run Code Online (Sandbox Code Playgroud)
表单.py
class UpdateProductForm(forms.Form):
name = forms.CharField(
max_length=200,
required=True,
label="* name:",
widget=TextInput(attrs={'class' : 'span6 small-margin-top small-margin-bottom'}),
)
description = forms.CharField(
max_length=200,
required=True,
label="* description:",
widget=TextInput(attrs={'class' : 'span6 small-margin-top small-margin-bottom'}),
)
price = forms.IntegerField(
label="* price:",
widget=TextInput(attrs={'class' : 'span6 small-margin-top small-margin-bottom'}),
)
qty = forms.IntegerField(
label="* Qty:",
widget=TextInput(attrs={'class' : 'span6 small-margin-top small-margin-bottom'}),
)
Run Code Online (Sandbox Code Playgroud)
查看.py
def update_risk(request,product_id):
product_details = get_product_details(product_id)
name = form.cleaned_data['name']
description = form.cleaned_data['description']
price = form.cleaned_data['price']
qty = form.cleaned_data['qty']
form …Run Code Online (Sandbox Code Playgroud)