django.shortcuts redirect doesn't pass argument

195 views
Skip to first unread message

forthfan

unread,
Aug 5, 2012, 9:21:20 PM8/5/12
to django...@googlegroups.com
Hi all,

I'm trying to pass a filepath from one view to another by using 'redirect' from django.shortcuts, but the argument is not getting passed.  What am I doing wrong?

def upload1(request):
  if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
      filepath = request.FILES['file']
      return redirect('/app/upload2/', filepath=filepath)

def upload2(request, filepath=None):
  if request.method == 'GET':
    if filepath:
    . . .

Karen Tracey

unread,
Aug 5, 2012, 11:42:14 PM8/5/12
to django...@googlegroups.com
On Sun, Aug 5, 2012 at 9:21 PM, forthfan <forthf...@gmail.com> wrote:

I'm trying to pass a filepath from one view to another by using 'redirect' from django.shortcuts, but the argument is not getting passed.  What am I doing wrong?

def upload1(request):
  if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
      filepath = request.FILES['file']
      return redirect('/app/upload2/', filepath=filepath)

You are passing the hardcoded url to redirect, which will be used as-is (per doc: https://docs.djangoproject.com/en/1.4/topics/http/shortcuts/#redirect). If you want the filepath kwarg to be used to construct the url specified in the redirect, you need to pass the name of the view (or url pattern) for the view.

Karen
--
http://tracey.org/kmt/

Bill Beal

unread,
Aug 6, 2012, 1:04:35 AM8/6/12
to django...@googlegroups.com
My example is wrong, because filepath turns out to be an 'InMemoryUploadedFile' object.  But the problem remains the same because I had a dummy filename string that I was trying to pass.  Actually the file would be uploaded to the server to a local file in upload1, then the filepath of the local file needs to be passed to upload2.

On Sun, Aug 5, 2012 at 9:21 PM, forthfan <forthf...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Bl7QVZZv7_MJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Bill Beal

unread,
Aug 6, 2012, 2:19:19 AM8/6/12
to django...@googlegroups.com
I see that I had mixed the second and third ways of using 'redirect' in the doc.  It seems to be working OK this way:

def upload1(request):
  . . .
      fout = 'tempfile.csv'
      return redirect('/isf/upload2/' + fout + '/')

def upload2(request, filename=None):
  . . .

with the following in urls.py:

(r'^app/upload2/(?P<filename>[A-Za-z0-9._-]+)/$', upload2),

Thanks.

I've been working on this while watching a live NASA feed of Curiosity's landing on Mars.  Exciting!


--
You received this message because you are subscribed to the Google Groups "Django users" group.
Reply all
Reply to author
Forward
0 new messages