문제

Is there any way to check if the format of an input in Django comments is correct? Specifically, I want to check that the email entered has the form @thisuniversity.edu? I want to make sure that all the comments come from people in the university, so this would be a way to check this.

I overwrote the code for the comments/form.html file to include specific functionality.

The specific file is below:

{% 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>

Thanks.

도움이 되었습니까?

해결책

I would clean the email field when the form is submitted. See the docs.

Sample code:

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')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top