Ejemplo n.º 1
0
def create_photo_view(request):
    if request.method == 'POST':
        form = PhotoForm(request.user, request.POST, request.FILES)
        if form.is_valid():
            photo = form.save(commit=False)
            photo.owner = request.user
            photo.save()
            # photo.image = form.cleaned_data.get('image')
            tags = form.cleaned_data.get('tags')
            for tag in tags:
                photo.tags.add(tag)
            photo.save()
            return redirect(photo_view, photo.pk)
    else:
        context = {'photo_form': PhotoForm(request.user)}
        return render(request, 'create_photo.html', context)
Ejemplo n.º 2
0
def edit_photo_view(request, photo_id):
    try:
        photo = Photo.objects.get(pk=photo_id)
    except Photo.DoesNotExist:
        raise Http404
    if request.method == 'POST':
        form = PhotoForm(request.user, request.POST, instance=photo)
        if form.is_valid():
            update_photo = form.save(commit=False)
            tags = form.cleaned_data.get('tags')
            for tag in tags:
                update_photo.tags.add(tag)
            update_photo.save()
            return redirect(photo_view, photo.pk)
    else:
        context = {'edit_photo_form': PhotoForm(request.user, instance=photo), 'photo': photo}
        # photo_form = PhotoForm(request.user, instance=photo)
        # photo_form.fields['albums'].queryset
        # context = {'edit_photo_form': photo_form, 'photo': photo}
        return render(request, 'edit_photo.html', context)