Django modelform已提交,但数据未保存?
好了,伙计们,我有一个自定义的用户注册表单,每当提交表单时,就会弹出一个浏览器警报,说“127.0.0.1:8000的页面上写着:已提交!”,但什么也没发生。不保存任何数据。以下是我试图保存表单的索引视图的代码:Django modelform已提交,但数据未保存?,django,Django,好了,伙计们,我有一个自定义的用户注册表单,每当提交表单时,就会弹出一个浏览器警报,说“127.0.0.1:8000的页面上写着:已提交!”,但什么也没发生。不保存任何数据。以下是我试图保存表单的索引视图的代码: def Index(request): """View to serve the index page.""" regform = models.RegistrationForm() loginform = models.LoginForm() if request.m
def Index(request):
"""View to serve the index page."""
regform = models.RegistrationForm()
loginform = models.LoginForm()
if request.method == 'POST':
if 'signup' in request.POST:
form = models.RegistrationForm(request.POST)
if form.is_valid():
form.save()
message = 'Your Account has been created. You may login now.'
return shortcuts.render_to_response(
'index.html', {'message' : message, 'regform' : regform, 'loginform' : loginform})
else:
message = 'Error: Please fill in all the fields and try again'
return shortcuts.render_to_response(
'index.html', {'regform' : regform, 'message' : message, 'loginform' : loginform})
elif 'login' in request.POST:
requser = request.POST['loginemail']
reqpass = request.POST['loginpass']
'''check if email exists'''
emailexist = models.member.objects.filter(Email=requser).count()
if emailexist == 1:
exists = True
else:
exists = False
'''if email exists check for password'''
if exists == True:
mem = models.member.objects.get(Email=requser)
if reqpass == mem.Password:
request.session['member_id'] = mem.id
return shortcuts.render_to_response(
'login.html')
else:
error = 'You entered an invalid Password, Please try again.'
return shortcuts.render_to_response(
'index.html', {'error' : error, 'regform' : regform, 'loginform' : loginform})
else:
error = 'That E-mail Address is Not Registered, Please Check the spelling and try again.'
return shortcuts.render_to_response(
'index.html', {'regform' : regform, 'loginform' : loginform, 'error' : error})
else:
return shortcuts.render_to_response(
'index.html', {'regform' : regform, 'loginform' : loginform})
对不起,这是型号代码和登记表
型号:
class member(models.Model):
"""Model to represent a User."""
First_Name = models.CharField(max_length=100, blank=False)
Last_Name = models.CharField(max_length=100, blank=False)
Stage_Name = models.CharField(max_length=100, unique=True, blank=False)
Account_Type = models.CharField(max_length=200, choices=USERTYPE_CHOICES, blank=False)
Password = models.CharField(max_length=100, blank=False)
Email = models.EmailField(max_length=100, blank=False)
Sex = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=False)
Birthday = models.CharField(max_length=2, blank=False)
def __unicode__(self):
return self.Email
登记表格:
class RegistrationForm(forms.ModelForm):
Stage_Name = forms.CharField(label='Username')
Email = forms.CharField(initial='you@example.com')
Birthday = forms.CharField(initial='dd/mm/yyyy')
Password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model=member
下面是模板:
{% extends 'base.html' %}
{% load login_checkbox %}
{% block topbar %}
<head>
</head>
<body>
<div id="logo" class="left"><a href="http://www.muzikube.com/"><img src="/static/images/logo.png" alt="Muzikube" width="200" height="52" border="0"/></a></div>
<div id="login" class="right">
<form id="loginform" action="." method="post">
{% for field in loginform %}
{% if field.field.widget|is_checkbox %}
<div class="checkbox">{{field}}{{field.label_tag}}<span id="forgotpass"><a href="/forgot/">Can't Sign In?</a></span></div>
{% else %}
{{field.label_tag}}{{field}}
{% endif %}
{% endfor %}
<input id="submit" class="submit" "type="submit" name="login" value="In!" />
</form>
{% if error %}
{{error}}
{% endif %}
</div>
<div class="clear"></div>
</body>
{% endblock topbar %}
{% block content %}
<body>
<div class="left">
<div id="heading">Connect To Other Musicians</div>
<div class="subheading">Upload your music, Share Pictures, Share Videos, Make Friends, Get Known !</div>
<div id="homepageimage"><img src="/static/images/comm.png" alt="Connect to other Musicians Around the world!" width="450" height=""/></div>
<div class="subheading">Muzikube let's you connect to other people around the world that share same interests as you !</div>
</div>
<div id="registrationform" class="right">
<div id="form-title">Sign Up, It's Free !</div>
<div id="form-subtitle">Connect to other musicians around the world.</div>
<div class="border"></div>
<div class="error">
{% if message %}
{{message}}
{% endif %}
</div>
<form id="regform" action="." method="post">
<table>
{{ regform.as_table }}
</table>
<input id="register-submit" class="submit" type="submit" name="signup" value="Sign Up" />
</form>
<div class="border"></div>
</div>
<div class="clear"></div>
</body>
{% endblock content %}
{%extends'base.html%}
{%load login_checkbox%}
{%block topbar%}
{loginform%中字段的%s}
{%if-field.field.widget |是_-checkbox%}
{{field}}{{field.label_tag}
{%else%}
{{field.label_tag}{{{field}}
{%endif%}
{%endfor%}
从上面的代码中很难分辨出来,但我猜你会得到一篇没有“登录”或“注册”的帖子(我不明白为什么它会有来自代码中的那些参数,可能是html中的参数?),因此帖子请求不会保存或返回任何内容 我们得看看你的登记表。它还将有助于查看“已提交”警报的位置/内容!好的,让我截图并上传它!不,不,我的意思是,代码的哪一部分正在发送警告“已提交”的HTML?(很难说要查看代码的哪一部分)我假设form.is_valid()返回True,因此我们应该查看您的表单?这也是整件事的原因吗?返回自我。电子邮件看起来不合适。是的,这就是整个注册表格,该死的,我不小心删除了我的模型。py,,lol,return self.Email
怎么了?还有一个重要的问题是:form.is\u valid()
肯定会返回True吗?是的,基本上我在一个视图中有两个表单,所以我试图从提交按钮中捕获名称,所以注册表的提交按钮名为“注册”。同样有趣的是,上面视图中的登录表单工作得非常完美是的,我现在肯定会发布它,,,事实上我有一个想法,,,这是jquery验证插件,导致了警报事件,,,好的,我已经在问题中添加了模板代码,,,我正在使用jquery验证插件,验证重新注册表单,我想这就是造成这个问题的原因…为每个表单添加一个名为“login”和“signup”的隐藏表单。表单本身没有与表单一起发送,因此看起来您缺少它。