Django form redirect using HttpResponseRedirect -
So it can not be very difficult but I can not understand it ...
I To upload the file, I want my form in the dynamic (located at / file_upload / ), add it to the database, and then redirect to a new page where the parameter is the id of the field ( Is located in / file / 163 / ).
I have set up the URLs so that / file / 163 / works fine if you navigate directly there, but I do not know / File / upload /
My code is:
def add (request): If request.method == 'POST': # If the form is submitted ... form = UploadFile Form (Request.POST, request.FILES) If Form Is_valid (): #DooStuff & amp; Add to database my_file = FileField.objects.create () Returns HttpResponseRedirect (reverse ('/ file /', args = [my_file.id]))
I can not use it because I do not know that the field ID is going to happen until I manage the form in view.py, so the redirect should be in views.py. I guess
Any thoughts?
Your view should appear something like this:
Def add (request): If request.method == 'POST': form = UploadFileForm (request.POST, request.FILES) if form.is_valid (): # do stuff & amp; Use my_file = FileField.objects.create () # my_file.pk or any feature of FileField is based on your ID return HttpResponseRedirect ('/ files /% i /'% my_file.pk) Other: form = UploadFileForm () Returns render_to_response ('upload_file.html', {'form': form,})
Comments
Post a Comment