помогите понять, почему прикрепленные файлы не сохраняются?
моя вьюшка:
class CreateAndUpdateAdv(DecoratorChainingMixin, FormView):
decorators = [login_required]
form_class = AdvForm
model = Adv
template_name = 'adv/form_adv.jinja'
success_url = reverse_lazy('adv-cat')
def form_valid(self, form):
self.pk = self.kwargs.get("pk")
if self.pk == None:
Adv.objects.create(**form.cleaned_data)
else:
Adv.objects.filter(pk=self.pk).update(**form.cleaned_data)
return redirect(self.get_success_url())
def get_form_kwargs(self):
self.pk = self.kwargs.get("pk")
if not self.pk == None:
self.adv = Adv.objects.get(pk=self.kwargs.get("pk"))
kwargs = super(CreateAndUpdateAdv, self).get_form_kwargs()
kwargs.update({'instance': self.adv})
else:
kwargs = {'initial': self.get_initial()}
self.adv = Adv.DoesNotExist
kwargs = super(CreateAndUpdateAdv, self).get_form_kwargs()
return kwargs
модель:
def attachment_upload_to(instance, filename):
now = datetime.today()
return u"attachments/%s/%s/%s/%s/%s" % (now.year, now.month, now.day, now.hour, filename)
class Adv(models.Model):
title = models.CharField(U'Название объявления', max_length=255)
slug = models.SlugField(U'URL - относительный путь', max_length=255)
category = models.ForeignKey(Category, verbose_name=u'Категория для объявления', blank=True)
content = models.TextField(u'Контент')
img1 = models.ImageField(u"картинка №1", upload_to=attachment_upload_to, null=True, blank=True)
img2 = models.ImageField(u"картинка №2", upload_to=attachment_upload_to, null=True, blank=True)
img3 = models.ImageField(u"картинка №3", upload_to=attachment_upload_to, null=True, blank=True)
img4 = models.ImageField(u"картинка №4", upload_to=attachment_upload_to, null=True, blank=True)
img5 = models.ImageField(u"картинка №5", upload_to=attachment_upload_to, null=True, blank=True)
форма:
class Adv(models.Model):
title = models.CharField(U'Название объявления', max_length=255)
slug = models.SlugField(U'URL - относительный путь', max_length=255)
category = models.ForeignKey(Category, verbose_name=u'Категория для объявления', blank=True)
content = models.TextField(u'Контент')
img1 = models.ImageField(u"картинка №1", upload_to=attachment_upload_to, null=True, blank=True)
img2 = models.ImageField(u"картинка №2", upload_to=attachment_upload_to, null=True, blank=True)
img3 = models.ImageField(u"картинка №3", upload_to=attachment_upload_to, null=True, blank=True)
img4 = models.ImageField(u"картинка №4", upload_to=attachment_upload_to, null=True, blank=True)
img5 = models.ImageField(u"картинка №5", upload_to=attachment_upload_to, null=True, blank=True)
в setting.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + MEDIA_URL
почему файлы не сохраняются ? для меня в функциональном подходе понятно
как работает
def textnote_edit(request, id):
note = TextNote.objects.get(id=id)
form = TextNoteForm(request.POST or None)
if request.method == 'POST':
form = TextNoteForm(request.POST, request.FILES)
if form.is_valid():
note.title = form.cleaned_data.get('title')
note.image = form.cleaned_data.get('image', None)
note.content = form.cleaned_data.get('content')
note.save()
return HttpResponseRedirect('/list/'+ id +'/')
else:
form = TextNoteForm(request.POST or None)
form.initial.update({'title': note.title,
'image': note.image,
'content': note.content
})
else:
form.initial.update({'title': note.title,
'image': note.image,
'content': note.content
})
return render(request, 'textnotes/textnoteedit.html', {"form": form, 'note': note})