我是Django的新手,它试图创建一个包含两个提交按钮的网页,一个用于登录,另一个用于注册。 为数据库添加模型类后,发生此错误。 (预模型代码以符号#注释)。 在网上或在Django文档中找不到此问题的答案。
authentication.html
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom-mb-4">Login User</legend>
{{Lform|crispy}}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit" name="login">Login</button>
</div>
</form>
</div>
</div>
<div class="col-md-6">
<div class="content-selection">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom-mb-4">Register User</legend>
{{Rform|crispy}}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit" name="register">Register</button>
</div>
</form>
下面是views.py
views.py
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import login, register
def home(request):
return render(request,"authentication/home.html",{})
def authentication(request):
if request.method=='POST':
Lform=login(request.POST)
Rform=register(request.POST)
if Lform.is_valid():
email = Lform.cleaned_data.get('Email')
password = Lform.cleaned_data.get('Password')
messages.success(request,email+' loged in')
return redirect('home')
elif Rform.is_valid():
name=Rform.cleaned_data.get('name')
email = Rform.cleaned_data.get('email')
password = hash(Rform.cleaned_data.get('password'))
confirm_password = hash(Rform.cleaned_data.get('confirm_password'))
if password == confirm_password:
messages.success(request,name+' registered')
return redirect('home')
else:
messages.error(request,'password dont match')
return redirect('authentication')
else:
print('page')
Lform=login()
Rform=register()
return render(request,"authentication/authentication.html", {'Lform':Lform,'Rform':Rform})
forms.py
from django import forms
from .models import Authentication
class login(forms.Form):
Email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Email'}))###Capital E in email for login
Password = forms.CharField(widget=forms.PasswordInput())###Capital P in Password for login
class register(forms.Form,forms.ModelForm):
class meta:
model = Authentication
fields = '__all__'
widgets = {
'name': forms.TextInput(attrs={'placeholder': 'Name'}),
'email': forms.TextInput(attrs={'placeholder': 'Email'}),
'password': forms.PasswordInput(),
}
labels = {
'name':'Name',
'email':'Email-id',
'password':'Password',
}
####name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Name'}))
####email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder':'Email'}))
#####password = forms.CharField(widget=forms.PasswordInput())
confirm_password =forms.CharField(widget=forms.PasswordInput())
def __init__(self, *args, **kwargs):
super(register, self).__init__(*args, **kwargs)
self.fields['confirm_password'].label = "Confirm Password"
models.py
from django.db import models
class Authentication(models.Model):
name = models.CharField(max_length=20)
email = models.EmailField(primary_key = True,max_length=20)
password = models.CharField(max_length=20)
def __str__(self):
return self.name
Request Method: GET
Request URL: http://localhost:8000/auth/
Django Version: 2.1.7
Exception Type: ValueError
Exception Value:
ModelForm has no model class specified.
Exception Location: D:\Python\lib\site-packages\django\forms\models.py in __init__, line 285
Python Executable: D:\Python\Scripts\python.exe
Python Version: 3.7.2''''
''''D:\Python\login\authentication\views.py in authentication
Rform=register() ...
▶ Local vars
D:\Python\lib\site-packages\django\forms\models.py in __init__
raise ValueError('ModelForm has no model class specified.') ...
▶ Local vars
我还有另一个问题,即要在模型中添加登录按钮。为此,我必须在forms.py类登录名中更改名称。从models.py获取属性时,在forms.py中获取输入时,无法找出用于更改字段名称的元标记。