自定义error_messages。 Django 1.8.6

时间:2015-11-24 11:34:44

标签: python django

这一天,我已经阅读了有关覆盖表单默认error_messages的大量信息,我无法处理它。我不知道我的代码有什么问题以及为什么我的自定义消息不起作用:

这是我的views.py

### views.py ###
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from django.views import generic
from django.template import RequestContext
from django.http import JsonResponse

from comments.models import Comment
from companies.models import Company

# Create your views here.

class IndexView(generic.ListView):
    model = Comment

    def get_queryset(self):
        return  Comment.objects.filter(company = self.kwargs['pk']).order_by('-add_date')

class AjaxAdd(object):
    def form_invalid(self, form):
        response = super(AjaxAdd, self).form_invalid(form)
        if self.request.is_ajax():
            return JsonResponse(form.errors, status = 400)
        else:
            return response

    def form_valid(self, form):
        form.instance.company = Company.objects.get(pk = self.kwargs['company'])
        response = super(AjaxAdd, self).form_valid(form)
        if self.request.is_ajax():
            five_comments_list = Comment.objects.order_by('-add_date').filter(company = self.object.company)
            data = {}
            data['five_comments_list'] = five_comments_list

            return render_to_response('companies/includes/_five_comments_list.html', data, context_instance=RequestContext(self.request))
        else:
            return response

class AddView(AjaxAdd, generic.edit.CreateView):
    model = Comment
    fields = ['text']

    def get_success_url(self):
        return reverse('companies:detail', args = (self.kwargs['company'],))

class AjaxDelete(object):
    def dispatch(self, *args, **kwargs):
        response = super(AjaxDelete, self).dispatch(*args, **kwargs)
        if self.request.is_ajax():
            five_comments_list = Comment.objects.order_by('-add_date').filter(company = self.object.company)
            data = {}
            data['five_comments_list'] = five_comments_list
            return render_to_response('companies/includes/_five_comments_list.html', data, context_instance=RequestContext(self.request))
        else:
            return response

class DeleteView(AjaxDelete, generic.edit.DeleteView):
    model = Comment

    def get_success_url(self):
        return reverse('companies:detail', args = (self.kwargs['company'],))

这是我的forms.py

### forms.py ###
# -*- coding: utf-8 -*-

from django import forms
from django.forms import ModelForm
from django.utils.translation import ugettext as _


from comments.models import Comment

class CommentForm(ModelForm):

    class Meta:
        model = Comment
        fields = ['text',]

        labels = {
            'text': '',
        }

        error_messages = {
            'text': {
                'required': _("lknlkjnlkjnl"),
            },
        }

        widgets = {
            'text': forms.Textarea(
                attrs = {
                    'id': 'text-comment-input',
                    'placeholder': 'Wpisz tu treść zgłoszenia...',
                }
            ),
        }

    def __init__(self, *args, **kwargs):
        self.fields['text'].error_messages = {'required': 'First Name is Required'}

我正在调用“公司”应用添加评论操作,如下所示:

###company view.py
class DetailView(generic.DetailView):
    model = Company

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        context['five_comments_list'] = self.object.get_latest_comments()
        context['add_comment_form'] = CommentForm
        return context

和模板:

###company_detail.html

<form id="add-comment-form" method="post" action="{% url 'comments:add' company.id %}">
  {% csrf_token %}
  {{ add_comment_form }}
  <input type="submit" value="Dodaj">
</form>

<div id="company-comments">
    {% include 'companies/includes/_five_comments_list.html' %}
</div>

我的javascript:

function AddComment() {

  handle_submit = function(event) {
    event.preventDefault();

    $.ajax({
      url : $('#add-comment-form').attr('action'),
      type : $('#add-comment-form').attr('method'),
      data : {
        text : $('#text-comment-input').val(),
      },

      success : function(json) {
        $('#company-comments').empty();
        $('#company-comments').prepend(json);
      },

      error : function(xhr, errmsg, err) {

        console.log(xhr.responseText);
        /*var old_placeholder_text = 'Wpisz tu treść zgłoszenia...';
        var new_placeholder_text = JSON.parse(xhr.responseText).text[0];

        $('textarea').attr('placeholder', new_placeholder_text);
                $('textarea').addClass('form-error');

                $(document).on('click', 'textarea', function(){
                    $('textarea').removeClass('form-error');
                    $('textarea').attr('placeholder', old_placeholder_text);
                });*/
      }
    });
  };

  init = function() {
    $(document).on('submit', '#add-comment-form', handle_submit);
  };
  init();
};

function DeleteComment() {

  handle_submit = function(event) {
    event.preventDefault();

    $.ajax({
      url : $('#delete-comment-form').attr('action'),
      type : $('#delete-comment-form').attr('method'),

      success : function(json) {
        $('#company-comments').empty();
        $('#company-comments').prepend(json);
      },

      error : function(xhr, errmsg, err) {
        console.log(xhr.status + ': ' + xhr.responseText);
      }
    });
  };

  init = function() {
    $(document).on('submit', '#delete-comment-form', handle_submit);
  };
  init();
};

$(document).ready(function() {

  new AddComment();
  new DeleteComment();

});

1 个答案:

答案 0 :(得分:1)

例如,您需要通过设置form_class告诉Django在视图中使用您的自定义表单。

class AddView(AjaxAdd, generic.edit.CreateView):
    model = Comment
    form_class = CommentForm

您的__init__方法看起来不对,因为它没有调用super()。我将从__init__方法中删除以下行,它将撤消自定义。

       self.fields['text'].error_messages = {'required': 'First Name is Required'}

如果这是__init__方法中的唯一内容,则将其完全删除。

一个工作示例

当我提交以下空表单时,我看到自定义错误消息。它似乎也适用于ajax请求。

模型

class MyModel(models.Model):
    name = models.CharField(max_length=100)

模型表格

class MyModelForm(forms.ModelForm):

    class Meta:
        fields = '__all__'
        model = MyModel
        error_messages = {'name': {'required': 'Custom required error message'}}

查看

from django.views.generic.edit import UpdateView
from django.http import JsonResponse

class MyUpdateView(UpdateView):
    model = MyModel
    form_class = MyModelForm

    template_name = 'my_template.html'

    def form_invalid(self, form):
        response = super(MyUpdateView, self).form_invalid(form)
        if not self.request.is_ajax():
            return JsonResponse(form.errors, status = 400)
        else:
            return response

URL

urlpatterns = [
    url(r'^(?P<pk>\d+)/$', MyUpdateView.as_view()),
]