这是我的views.py文件。
from django.shortcuts import render, redirect
from .forms import RegistrationForm
def index(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'Login/index.html', args)
这是我的models.py文件
from django.db import models
class Customer(models.Model):
fname = models.CharField(max_length=25)
company = models.CharField(max_length=15)
email = models.CharField(max_length=15)
password1 = models.CharField(max_length=15)
password2 = models.CharField(max_length=15)
def __str__(self):
return self.company + ' - ' + self.fname
以下是我的forms.py文件
from django import forms
from django.forms import ModelForm
from .models import Customer
class RegistrationForm(forms.ModelForm):
email = forms.EmailField(required=True)
class Meta:
model = Customer
fields = (
'fname',
'company',
'email',
)
def save(self, commit=True):
user= super(RegistrationForm, self).save(commit=False)
user.UserName= self.cleaned_data.get['name']
user.CName = self.cleaned_data.get['cname']
user.Email = self.cleaned_data.get['email']
if commit:
user.save()
return user
HTML文件是:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.ico"/>
<title>Customer Desk</title>
{% load static %}
<link href="{% static 'css/Styles.css' %}" rel="stylesheet" type="text/css"
/>
</head>
<body class="login">
<!-- header starts here -->
<div id="desk-Bar">
<div id="desk-Frame">
<div id="logo"> <a href="http://127.0.0.1:8000/Home/">Virtual Agent
</a></div>
<div id="header-main-right">
<div id="header-main-right-nav">
<form method="post" action="" id="login_form" name="login_form">
<table border="0" style="border:none">
<tr>
<td ><input type="text" tabindex="1" id="mail"
placeholder="Email or Phone" name="email" class="inputtext radius1"
value=""> </td>
<td ><input type="password" tabindex="2" id="pass"
placeholder="Password" name="pass" class="inputtext radius1" ></td>
<td ><input type="submit" class="fbbutton" name="login"
value="Login" /></td>
</tr>
<tr>
<td><label>
<input id="persist_box" type="checkbox" name="persistent" value="1" checked="1"/>
<span style="color:#ccc;">Keep me logged in</span></label>
</td>
<td><label><a href="" style="color:#ccc; text-decoration:none">forgot your password?</a></label></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
<!-- header ends here -->
<table>
<tr>
<td colspan="6">
<div class="loginbox radius">
<h2 style="color:#336; text-align:center;">Welcome to Virtual
Agent</h2>
<div class="loginboxinner radius">
<div class="loginheader">
<h4 class="title"></h4>
</div>
<!--loginheader-->
ejiofhndouiwefhnouiewnhfounhefoufnheohu3jogsdhfbviurehjnmihmeju hguihg
9uhe39u8gt 43y3489yt398thy983gt3hy4988498gty4h
<!--loginform-->
</div>
<!--loginboxinner-->
</div>
<!--loginbox-->
</td>
<td>
<div class="loginbox radius">
<h2 style="color:#336; text-align:center;">Welcome to Virtual
Agent</h2>
<div class="loginboxinner radius">
<div class="loginheader">
<h4 class="title">Connect with friends and the world around
you.</h4>
</div>
<!--loginheader-->
<div class="loginform">
<form id="login" action="" method="post">
{% csrf_token %}
<p>
<input type="text" id="name" name="name"
placeholder="Your Name" value="" class="radius mini" />
<input type="text" id="company" name="cname"
placeholder="Company Name" value="" class="radius mini" />
</p>
<p>
<input type="text" id="email" name="email"
placeholder="Your Email" value="" class="radius" />
</p>
<p>
<input type="password" id="password" name="pass"
placeholder="New Password" class="radius" />
</p>
<p>
<input type="password" id="rpassword" name="rpass"
placeholder="Re-Enter Password" class="radius" />
</p>
<p>
<button class="radius title" name="signup">Sign Up
for Virtual Agent</button>
</p>
</form>
</div>
<!--loginform-->
</div>
<!--loginboxinner-->
</div>
<!--loginbox-->
</td>
</tr>
</table>
</body>
</html>
请有人帮助我。因为我已尽力注册一个人并在模型中保存数据。客户。但我不能。
答案 0 :(得分:1)
你应该使用Django admin的用户,并创建一个客户模型,如何通过一对一的关系与User相关,并在创建时使用信号,这可以帮助你:https://simpleisbetterthancomplex.com/tutorial/2016/11/23/how-to-add-user-profile-to-django-admin.html
答案 1 :(得分:0)
HTML文件中的字段与表单类中的字段完全不匹配。因此,表格无效;但是您没有显示可以显示错误的验证错误。
但是,即使您完成了这项工作,也不能这样做。您正在创建自己的用户模型,以纯文本格式保存密码。不要这样做。 Django有一个非常好的身份验证框架,允许您根据自己的喜好自定义模型;你应该使用它。