Monday, November 23, 2015

Custom error_messages

This day I have read the mass of information about overiding form default error_messages and I just can not handle it. I do not know what is wrong with my code and why my custom message does not work:

###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...',
               
}
           
),
       
}


###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 AjaxableResponseMixin(object):
   
def form_invalid(self, form):
        response
= super(AjaxableResponseMixin, 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(AjaxableResponseMixin, 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(AjaxableResponseMixin, generic.edit.CreateView):
    model
= Comment
    fields
= ['text']

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7620cae2-3f40-40a1-a06c-989213bdaf80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment