有没有办法检查Django评论中输入的格式是否正确?具体来说,我想检查输入的电子邮件是否为@ thisuniversity.edu?我想确保所有评论都来自大学里的人,所以这将是一种检查方法。
我覆盖了comments / form.html文件的代码以包含特定功能。
具体文件如下:
{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if next %}<div><input type="hidden" name="next" value="{{ next }}" /></div>{% endif %}
{% for field in form %}
{% if field.is_hidden %}
<div>{{ field }}</div>
{% else %}
{% if field.errors %}{{ field.errors }}{% endif %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
{% if field.name != "url" and field.name != "email" and field.name != "name" %}
{{ field.label_tag }} {{ field }}
{% elif field.name = "email" %}
<b>Enter your university email address.</b><br><br>
{{ field.label_tag }} {{ field }}
{% elif field.name = "name" %}
{{ field.label_tag }} {{ field }}
{% endif %}
</p>
{% endif %}
{% endfor %}
<p class="submit">
<input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" />
</p>
</form>
感谢。
答案 0 :(得分:0)
我会在提交表单时清理电子邮件字段。 See the docs
示例代码:
class SomeForm(forms.Form):
# Everything as before.
def clean_email(self):
email = self.cleaned_data.get('email', '')
if email.endswith('@something.com'):
return email
raise forms.ValidationError('Provide an Something.com email address')