mirror of https://github.com/django/django.git
Fixed #15287 -- Added translation markers to user-facing error messages in class-based generic views. Thanks to szczav for the report and draft patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15533 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
bb26c328ec
commit
72e4a6865d
|
@ -3,11 +3,12 @@ import datetime
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
from django.utils.encoding import force_unicode
|
||||||
|
from django.utils.translation import ugettext as _
|
||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin
|
from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin
|
||||||
from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin
|
from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin
|
||||||
|
|
||||||
|
|
||||||
class YearMixin(object):
|
class YearMixin(object):
|
||||||
year_format = '%Y'
|
year_format = '%Y'
|
||||||
year = None
|
year = None
|
||||||
|
@ -29,7 +30,7 @@ class YearMixin(object):
|
||||||
try:
|
try:
|
||||||
year = self.request.GET['year']
|
year = self.request.GET['year']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise Http404("No year specified")
|
raise Http404(_(u"No year specified"))
|
||||||
return year
|
return year
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,7 +55,7 @@ class MonthMixin(object):
|
||||||
try:
|
try:
|
||||||
month = self.request.GET['month']
|
month = self.request.GET['month']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise Http404("No month specified")
|
raise Http404(_(u"No month specified"))
|
||||||
return month
|
return month
|
||||||
|
|
||||||
def get_next_month(self, date):
|
def get_next_month(self, date):
|
||||||
|
@ -95,7 +96,7 @@ class DayMixin(object):
|
||||||
try:
|
try:
|
||||||
day = self.request.GET['day']
|
day = self.request.GET['day']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise Http404("No day specified")
|
raise Http404(_(u"No day specified"))
|
||||||
return day
|
return day
|
||||||
|
|
||||||
def get_next_day(self, date):
|
def get_next_day(self, date):
|
||||||
|
@ -134,7 +135,7 @@ class WeekMixin(object):
|
||||||
try:
|
try:
|
||||||
week = self.request.GET['week']
|
week = self.request.GET['week']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise Http404("No week specified")
|
raise Http404(_(u"No week specified"))
|
||||||
return week
|
return week
|
||||||
|
|
||||||
|
|
||||||
|
@ -194,7 +195,9 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
|
||||||
qs = qs.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
|
qs = qs.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
|
||||||
|
|
||||||
if not allow_empty and not qs:
|
if not allow_empty and not qs:
|
||||||
raise Http404(u"No %s available" % unicode(qs.model._meta.verbose_name_plural))
|
raise Http404(_(u"No %(verbose_name_plural)s available") % {
|
||||||
|
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
|
||||||
|
})
|
||||||
|
|
||||||
return qs
|
return qs
|
||||||
|
|
||||||
|
@ -208,7 +211,9 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
|
||||||
|
|
||||||
date_list = queryset.dates(date_field, date_type)[::-1]
|
date_list = queryset.dates(date_field, date_type)[::-1]
|
||||||
if date_list is not None and not date_list and not allow_empty:
|
if date_list is not None and not date_list and not allow_empty:
|
||||||
raise Http404(u"No %s available" % unicode(qs.model._meta.verbose_name_plural))
|
raise Http404(_(u"No %(verbose_name_plural)s available") % {
|
||||||
|
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
|
||||||
|
})
|
||||||
|
|
||||||
return date_list
|
return date_list
|
||||||
|
|
||||||
|
@ -458,9 +463,10 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV
|
||||||
qs = self.get_queryset()
|
qs = self.get_queryset()
|
||||||
|
|
||||||
if not self.get_allow_future() and date > datetime.date.today():
|
if not self.get_allow_future() and date > datetime.date.today():
|
||||||
raise Http404("Future %s not available because %s.allow_future is False." % (
|
raise Http404(_(u"Future %(verbose_name_plural)s not available because %(class_name)s.allow_future is False.") % {
|
||||||
qs.model._meta.verbose_name_plural, self.__class__.__name__)
|
'verbose_name_plural': qs.model._meta.verbose_name_plural,
|
||||||
)
|
'class_name': self.__class__.__name__,
|
||||||
|
})
|
||||||
|
|
||||||
# Filter down a queryset from self.queryset using the date from the
|
# Filter down a queryset from self.queryset using the date from the
|
||||||
# URL. This'll get passed as the queryset to DetailView.get_object,
|
# URL. This'll get passed as the queryset to DetailView.get_object,
|
||||||
|
@ -491,7 +497,10 @@ def _date_from_string(year, year_format, month, month_format, day='', day_format
|
||||||
try:
|
try:
|
||||||
return datetime.date(*time.strptime(datestr, format)[:3])
|
return datetime.date(*time.strptime(datestr, format)[:3])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise Http404(u"Invalid date string '%s' given format '%s'" % (datestr, format))
|
raise Http404(_(u"Invalid date string '%(datestr)s' given format '%(format)s'") % {
|
||||||
|
'datestr': datestr,
|
||||||
|
'format': format,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _month_bounds(date):
|
def _month_bounds(date):
|
||||||
|
|
|
@ -3,6 +3,7 @@ import re
|
||||||
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils.encoding import smart_str
|
from django.utils.encoding import smart_str
|
||||||
|
from django.utils.translation import ugettext as _
|
||||||
from django.views.generic.base import TemplateResponseMixin, View
|
from django.views.generic.base import TemplateResponseMixin, View
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,8 +48,8 @@ class SingleObjectMixin(object):
|
||||||
try:
|
try:
|
||||||
obj = queryset.get()
|
obj = queryset.get()
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
raise Http404(u"No %s found matching the query" %
|
raise Http404(_(u"No %(verbose_name)s found matching the query") %
|
||||||
(queryset.model._meta.verbose_name))
|
{'verbose_name': queryset.model._meta.verbose_name})
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
|
|
|
@ -4,6 +4,7 @@ from django.core.paginator import Paginator, InvalidPage
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.utils.encoding import smart_str
|
from django.utils.encoding import smart_str
|
||||||
|
from django.utils.translation import ugettext as _
|
||||||
from django.views.generic.base import TemplateResponseMixin, View
|
from django.views.generic.base import TemplateResponseMixin, View
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,12 +44,14 @@ class MultipleObjectMixin(object):
|
||||||
if page == 'last':
|
if page == 'last':
|
||||||
page_number = paginator.num_pages
|
page_number = paginator.num_pages
|
||||||
else:
|
else:
|
||||||
raise Http404("Page is not 'last', nor can it be converted to an int.")
|
raise Http404(_(u"Page is not 'last', nor can it be converted to an int."))
|
||||||
try:
|
try:
|
||||||
page = paginator.page(page_number)
|
page = paginator.page(page_number)
|
||||||
return (paginator, page, page.object_list, True)
|
return (paginator, page, page.object_list, True)
|
||||||
except InvalidPage:
|
except InvalidPage:
|
||||||
raise Http404(u'Invalid page (%s)' % page_number)
|
raise Http404(_(u'Invalid page (%(page_number)s)') % {
|
||||||
|
'page_number': page_number
|
||||||
|
})
|
||||||
|
|
||||||
def get_paginate_by(self, queryset):
|
def get_paginate_by(self, queryset):
|
||||||
"""
|
"""
|
||||||
|
@ -113,8 +116,8 @@ class BaseListView(MultipleObjectMixin, View):
|
||||||
self.object_list = self.get_queryset()
|
self.object_list = self.get_queryset()
|
||||||
allow_empty = self.get_allow_empty()
|
allow_empty = self.get_allow_empty()
|
||||||
if not allow_empty and len(self.object_list) == 0:
|
if not allow_empty and len(self.object_list) == 0:
|
||||||
raise Http404(u"Empty list and '%s.allow_empty' is False."
|
raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
|
||||||
% self.__class__.__name__)
|
% {'class_name': self.__class__.__name__})
|
||||||
context = self.get_context_data(object_list=self.object_list)
|
context = self.get_context_data(object_list=self.object_list)
|
||||||
return self.render_to_response(context)
|
return self.render_to_response(context)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue