0

当我输入无效的电子邮件地址时,我看不到“这不是无效的电子邮件”之类的错误消息。

我的comment_form.html:

{% load i18n %}

    <form action="/comment/create/" method="post">
        {% csrf_token %}
            {{ form.errors }}
            {% for field in form %}
            <div class="fieldWrapper">
                {{ field.label_tag }}
                {{ field }}<p>
            </div>

            {% endfor %}
            <input type="hidden" name = "object_id" value="{{object_id}}"  />
            <input type="hidden" name= "next" value="{{ next }}" />
        <input type="submit" value="{% trans "Submit" %}">

    </form>

我的 post_detail.html:

extends "base.html" %}



{% block content %}
<div id="exap">


    {{ post.content }}

</div>
<div class="comment">
    {% for comment in comments %}
        <user>{{ comment.owner }}</user>
        {{ comment.content}}
    {% endfor %}


</div>
{% include "comment_form.html" %}


{% endblock %}

这是我的评论。意见

   def create(request):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        email = request.POST.get('email')
        next = request.POST.get('next')
        if form.is_valid():
            content_type = ContentType.objects.get(app_label="post", model="post")
            object_id = request.POST["object_id"]
            comment = Comment.objects.create(
                content_type = content_type,
                object_id = object_id,
                content = request.POST.get('content'),
                owner= request.POST.get('owner'),
                email = request.POST.get('email')
            )
        else:
            print form.errors
    else:
        form = CommentForm()
    return HttpResponseRedirect(next)

错误信息不显示在模板中?

我哪里错了?

4

1 回答 1

1

karthikr 试图告诉你评论中有什么问题。您没有使用表单进行验证,因此它不会显示任何错误。而不是这样做if validateEmail(email),您应该在表单类中包含电子邮件验证代码,并且在您调用的视图中if form.is_valid()

另外,当您编写表单的clean_email方法时,您应该捕获 ValidationError:这是将错误传递到表单错误列表的方式。

于 2013-09-30T19:20:48.553 回答