Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form redirect using HttpResponseRedirect

So this can't be too hard but I can't figure it out...

I want my form in django (located at /file_upload/) to upload a file, add it to the database, and then redirect to a new page where the parameter is the id of the field that I've added in the database (located at /file/163/, say).

I've set up urls.py so that /file/163/ works just fine if you navigate there directly, but I don't know how to get there from /file/upload/.

My code is like this:

def add(request):
    if request.method == 'POST': # If the form has been submitted...
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            # do stuff & add to database
  my_file = FileField.objects.create()
            return HttpResponseRedirect(reverse('/file/', args=[my_file.id]))

I can't use this solution because I don't know what the field id is going to be until I've handled the form in views.py, so the redirect has to happen in views.py. I think.

Any thoughts?

like image 324
AP257 Avatar asked Oct 16 '09 10:10

AP257


People also ask

How to redirect a form in Django?

Django Redirects: A Super Simple Example Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .

When to use HttpResponseRedirect in Django?

HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.

How to redirect to another URL in Django views?

As already suggested by @mdegis you can use the Django redirect function to redirect to another view or url. You can pass positional or keyword argument(s) to the redirect shortcut using the reverse() method and the named url of the view you're redirecting to.

What is the difference between redirect and HttpResponseRedirect?

There is a difference between the two: In the case of HttpResponseRedirect the first argument can only be a url . redirect which will ultimately return a HttpResponseRedirect can accept a model , view , or url as it's "to" argument. So it is a little more flexible in what it can "redirect" to.


1 Answers

You've got the arguments to reverse wrong in

return HttpResponseRedirect(reverse('/file/', args=[my_file.id]))

reverse takes a view or the name or a view, not a url.

You don't say what your view function is called for viewing the file, but lets say it is called view, then the above should be

return HttpResponseRedirect(reverse('view', args=[my_file.id]))

or maybe

return HttpResponseRedirect(reverse(view, args=[my_file.id]))

Depending on exactly what you wrote in urls.py

You can name the views in urls.py and use those names instead of the function names- see the documentation for more examples

Using reverse is a good idea if you like shuffling your urls.py around - you'll be able to change any of the paths and all your views will just keep working.

like image 194
Nick Craig-Wood Avatar answered Oct 21 '22 15:10

Nick Craig-Wood