我正在尝试检查输入的电子邮件地址和表单用户名是否曾被使用过。代码适用于数据库中不存在电子邮件地址和用户名的情况,但是我在views.py中获得了无效的表单。我想在用户尝试提交时在表单中显示文本验证错误消息。
forms.py
class UpdateProfileForm(ModelForm):
class Meta:
model = UpdatedInformation
fields = ('email', 'username',)
def clean_email(self):
username = self.cleaned_data.get('username')
email = self.cleaned_data.get('email')
if email and User.objects.filter(email=email).exclude(username=username).count():
raise forms.ValidationError('This email address is already in use. Please supply a different email address.')
return email
def clean_username(self):
username = self.cleaned_data.get('username')
email = self.cleaned_data.get('email')
if email and User.objects.filter(username=username).exclude(email=email).count():
raise forms.ValidationError(
'This username is already in use. Please supply a different username.')
return username
views.py
@login_required
def update_profile(request, slug):
args = {}
#Check that the correct user has requested
if not slug == slugify(request.user.username):
raise Http404
#Get the user object from the database by its username
user = get_object_or_404(User, username=request.user.username)
#Create a new updated information object to initialise the form with
update_profile_obj = UpdatedInformation()
update_profile_obj.username = user.username
update_profile_obj.email = user.email
if request.method == 'POST':
form = UpdateProfileForm(request.POST)
print(form.errors)
if form.is_valid():
#Get the updated_info from the form
updated_info = form.save(commit=False)
#Update the user object with it
user.email = updated_info.email
user.username = updated_info.username
#Check if either the username and email address has already been used
if User.objects.filter(email=updated_info.email).exclude(username=updated_info.username).count():
raise Http404
if User.objects.filter(username=updated_info.username).exclude(email=updated_info.email).count():
raise Http404
#Save the updated user object within the database
user.save()
return redirect('account')
if not form.is_valid():
raise Http404
else:
form = UpdateProfileForm(instance=update_profile_obj)
args['form'] = form
return render(request, 'registration/update_profile.html', args)
html模板
{% extends 'base.html' %}
{% load staticfiles %}
{% load widget_tweaks %}
{% block content %}
<link rel="stylesheet" type="text/css" href="{% static 'css/update_profile_style.css' %}" />
<div class="update-profile-page-container">
<h2 class="update-profile-title text-center">EDIT DETAILS</h2>
<div class="update-form-container">
<form id="update-form" method="POST" class="form-inline" role="form">
{% csrf_token %}
<div class="row">
<h4 class="update-profile-subtitle">USERNAME</h4>
{{ form.username |attr:"class:form-control update-form-field" }}
</div>
<div class="row">
<h4 class="update-profile-subtitle">EMAIL</h4>
{{ form.email |attr:"class:form-control update-form-field" }}
</div>
<div class="row">
<div class = "container update-button-container" style="padding-left: 0;">
<a onclick="document.getElementById('update-form').submit();">
<input class="update-button center" type="button" value="SAVE">
</a>
</div>
</div>
</form>
</div>
</div>
{% endblock content %}
答案 0 :(得分:0)
您应该从views.py
if not form.is_valid():
raise Http404
这样做会返回HTTP 404状态,即“找不到页面”,这是错误的。如果表单无效,那么您只想使用绑定表单呈现响应,因此如果表单无效,删除这两行将执行对render
的调用。
此外,您应该删除视图中已使用的电子邮件/用户名的检查,因为如果表单有效,那么这些检查是多余的:
#Check if either the username and email address has already been used
if User.objects.filter(email=updated_info.email).exclude(username=updated_info.username).count():
raise Http404
if User.objects.filter(username=updated_info.username).exclude(email=updated_info.email).count():
raise Http404