Django
- 23 ответа
- 0 вопросов
15
Вклад в тег
class Advert(models.Model):
category = models.TextField()
title = models.CharField(max_length=64)
description = models.CharField(max_length=900)
date = models.DateTimeField(auto_now_add=True, null=True)
class AdvertImage(models.Model):
advert = models.ForeignKey(Advert)
photo = models.ImageField(upload_to='uploads/')
class AddAdvertForm(ModelForm):
class Meta:
model = Advert
fields = '__all__'
class AdvertImagesForm(forms.Form):
photos = forms.FileField(widget=widgets.FileInput(attrs={'multiple': True}))
def __init__(self, *args, **kwargs):
if 'request' in kwargs:
self.request = kwargs.pop('request')
super(AdvertImagesForm, self).__init__(*args, **kwargs)
def clean_photos(self):
# Остаются только картинки
photos = [photo for photo in self.request.FILES.getlist('photos') if 'image' in photo.content_type]
# Если среди загруженных файлов картинок нет, то исключение
if len(photos) == 0:
raise forms.ValidationError(u'Not found uploaded photos.')
return photos
def save_for(self, advert):
for photo in self.cleaned_data['photos']:
AdvertImage(photo=photo, advert=advert).save()
class CreateAdvertView(View):
template_name = 'bulletinboard/add_advert.html'
form_class = AddAdvertForm
form_images_class = AdvertImagesForm
model = Advert
def get(self, request):
form = self.form_class()
form_images = self.form_images_class()
return render(request, self.template_name, {'form': form, 'form_images': form_images})
def post(self, request):
form = self.form_class(request.POST)
form_images = self.form_images_class(request.POST, request.FILES, request=request)
if form.is_valid() and form_images.is_valid():
advert = form.save()
form_images.save_for(advert)
return HttpResponseRedirect('/')
return render(request, self.template_name, {'form': form, 'form_images': form_images})
sudo add-apt-repository ppa:attente/java-non-latin-shortcuts
sudo apt-get update
sudo apt-get dist-upgrade
restart unity-settings-daemon
from django.shortcuts import redirect
class RedirectPermissionRequiredMixin(PermissionRequiredMixin,):
login_url = reverse_lazy('dashboard')
def handle_no_permission(self):
return redirect(self.get_login_url())
# и примешать вместо PermissionRequiredMixin
class UserEditView(RedirectPermissionRequiredMixin, UpdateView):
template_name = 'users/edit_user.html'
form_class = UserEditForm
model = User
permission_required = ('auth.change_user')
{% block breadcrumbs %}
<div class="breadcrumb"><a href="/">Главная</a></div>
{% endblock %}
{% extends 'base.html' %}
{% block breadcrumbs %}
{{ block.super }}
<div class="breadcrumb"><a href="{% url 'articles:articles' %}">Каталог статей</a></div>
{% endblock %}
{% extends 'articles.html' %}
{% block breadcrumbs %}
{{ block.super }}
<div class="breadcrumb"><a href="{% url 'articles:article_view' article_id=article.id %}">{{ article.title }}</a></div>
{% endblock %}