Exemplo n.º 1
0
def create_album_view(request, user_name):
    if request.method == 'POST':
        form = AlbumForm(request.POST)
        if form.is_valid():
            album = form.save(commit=False)
            album.owner = request.user
            album.save()
            album.title = form.cleaned_data.get('title')
            photos = form.cleaned_data.get('photos')
            for photo in photos:
                album.photos.add(photo)
            album.save()
            return redirect(album_view, request.user.username, album.pk)
    else:
        context = {'album_form': NewAlbumForm()}
        return render(request, 'create_album.html', context)
Exemplo n.º 2
0
def edit_album_view(request, user_name, album_id):
    try:
        album = Album.objects.get(pk=album_id)
    except Album.AlbumNotFound:
        raise Http404
    if request.method == 'POST':
        form = AlbumForm(request.POST, instance=album)
        if form.is_valid():
            update_album = form.save(commit=False)
            update_album.title = form.cleaned_data.get('title')
            photos = form.cleaned_data.get('photos')
            for photo in photos:
                album.photos.add(photo)
            update_album.save()
            return redirect(album_view, request.user.username, album.pk)
    else:
        album_form = AlbumForm(instance=album)
        context = {'album_form': album_form, 'album': album}
        return render(request, 'edit_album.html', context)