Django
73
Вклад в тег
def messages(request):
return {
'messages': get_messages(request),
'DEFAULT_MESSAGE_LEVELS': DEFAULT_LEVELS,
}
...
def get_messages(request):
if hasattr(request, '_messages'):
return request._messages
else:
return []
class MessageMiddleware(MiddlewareMixin):
def process_request(self, request):
request._messages = default_storage(request)
...
def default_storage(request):
return import_string(settings.MESSAGE_STORAGE)(request)
...
MESSAGE_STORAGE = 'django.contrib.messages.storage.fallback.FallbackStorage'
messages.error(request, 'Ваш диапазон дат занят для данного объекта.')
def error(request, message, extra_tags='', fail_silently=False):
add_message(request, constants.ERROR, message, extra_tags=extra_tags,
fail_silently=fail_silently)
...
def add_message(request, level, message, extra_tags='', fail_silently=False):
if not isinstance(request, HttpRequest):
raise TypeError("add_message() argument must be an HttpRequest object, "
"not '%s'." % request.__class__.__name__)
if hasattr(request, '_messages'):
return request._messages.add(level, message, extra_tags) # *** THIS ***
if not fail_silently:
raise MessageFailure('You cannot add messages without installing '
'django.contrib.messages.middleware.MessageMiddleware')
def add(self, level, message, extra_tags=''):
if not message:
return
level = int(level)
if level < self.level:
return
# Add the message.
self.added_new = True
message = Message(level, message, extra_tags=extra_tags)
self._queued_messages.append(message)
...
def _get_level(self):
if not hasattr(self, '_level'):
self._level = getattr(settings, 'MESSAGE_LEVEL', constants.INFO)
return self._level
# settings.py
...
MY_SUPER_ERROR = 80
MESSAGE_LEVEL = MY_SUPER_ERROR
...
if not available_cars:
carstoshow = None
messages.add_message(request, settings.MY_SUPER_ERROR, 'Ваш диапазон дат занят для данного объекта.')
return redirect('car_detail', pk=car_used_id)
class Vospitately(models.Model):
fio = models.CharField(max_length=40)
dolshnost = models.CharField(max_length=80, default='Должность..')
image = models.ImageField()
vospet_crushek = models.ForeignKey(Cruski', related_name='vospitately')
def __str__(self):
return self.fio
def kruzhki(request):
q = Cruski.objects.all()
return render(request,'krushki.html',{'kruzki':q})
{% block kruzhki %}
{% for krug in kruzki %}
<p><b>{{ krug.name }}</b></p>
{% for vospet in krug.vospitately.all %}
<p> {{ vospet.fio }}</p>
{% endfor %}
<p>{{ krug.description }}</p>
<p>{{ krug.price }}</p>
{% endfor %}