def album_edit(request, falbum_id=None): album = None if falbum_id: album = Album.objects.get(id=falbum_id) PhotoFormSet = inlineformset_factory(Album, Photo, form=PhotoForm, fields=('image', 'title', 'meta_title', 'tags'), extra=3) if request.method == 'POST': form = AlbumForm(request.POST, instance=album) formset = PhotoFormSet(request.POST, request.FILES, instance=album) if form.is_valid() and formset.is_valid(): new_album = form.save(commit=False) new_album.author = request.user new_album.save() instances = formset.save(commit=False) for instance in instances: # do something with instance instance.author = request.user instance.album = new_album instance.save() return HttpResponseRedirect('/gallery/') else: form = AlbumForm(instance=album) formset = PhotoFormSet(instance=album) return direct_to_template(request, 'fgallery/album_edit.html', {'album': album, 'form':form, 'formset': formset})
def album_edit(request, album_id=None): album = None if album_id: album = Album.objects.get(pk=album_id) if request.method == 'POST': forma = AlbumForm(request.POST, instance=album) formset = EditPhotoFormSet(request.POST, request.FILES, instance=album) if forma.is_valid() and formset.is_valid(): if album == None: album = Album() album.author = request.user album.title = forma.cleaned_data['title'] album.slug = forma.cleaned_data['slug'] album.save() for form in formset.forms: if form.has_changed(): new_image = form.save(commit=False) new_image.album = album new_image.save() return HttpResponseRedirect(album.get_absolute_url()) else: forma = AlbumForm(instance=album) formset = EditPhotoFormSet(instance=album) return direct_to_template(request, 'fgallery/album_new.html', { 'forma': forma, 'formset': formset, 'album': album })
def album_edit(request, album_id=None): album = None if album_id: album = Album.objects.get(pk=album_id) if request.method == "POST": forma = AlbumForm(request.POST, instance=album) formset = EditPhotoFormSet(request.POST, request.FILES, instance=album) if forma.is_valid() and formset.is_valid(): if album == None: album = Album() album.author = request.user album.title = forma.cleaned_data["title"] album.slug = forma.cleaned_data["slug"] album.save() for form in formset.forms: if form.has_changed(): new_image = form.save(commit=False) new_image.album = album new_image.save() return HttpResponseRedirect(album.get_absolute_url()) else: forma = AlbumForm(instance=album) formset = EditPhotoFormSet(instance=album) return direct_to_template(request, "fgallery/album_new.html", {"forma": forma, "formset": formset, "album": album})