Skip to content Skip to sidebar Skip to footer

Passing Information Between Web Pages In Django

I have an Image Gallery System where I'm building a feature to edit the attributes of an uploaded image. @login_required def edit(request): if request.method == 'POST':

Solution 1:

You could solve this be passing the ZSN or the Image PK as an URL parameter to the next view. You need to do that because the actual Image instance can not be passed to the next view directly.

For example:

urls.py

from . import views
urlpatterns = [
    url(r'^home/edit/$', views.edit, name='edit'),
    url(r'^home/photo-edit/(?P<photo_id>[0-9]+)/$', views.photo_edit, name='photo-edit'),
]

views.py

defedit(request):
    if request.method == 'POST':
        ...
        image = Images.objects.filter(...).first()
        if image isnotNone:
            return redirect('photo-edit', image.pk)
        else:
            return HttpResponse("Invalid ZSN.")
    else:
        return render(request, 'cms/edit.html')

defphoto_edit(request, image_pk):
    image = get_object_or_404(Image, pk=image_pk)
    ...

Note how in this example the line redirect('photo-edit', image.pk) passes the image PK to the next view. Now you would just need to implement the view photo_edit to fit your use case.

Let us know if that brings you closer to solving your problem.

Post a Comment for "Passing Information Between Web Pages In Django"