1

Hi im trying to redirect from one view function to another and passing a list of lists as argument.

urls.py

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^checkFiles/', views.checkFiles, name='checkoutFiles'),
    url(r'^$', views.home, name='home'),
    url(r'^upload/', views.upload, name='upload'),
    url(r'^login', views.loginview, name='loginview'),
    url(r'^logout/', views.logoutview, name='logoutview'),
    url(r'^confirm_files/', views.upload, name='confirm_file'),
    url(r'^upload/', views.del_files, name='del_files'),

]

views.py

for redirecting from views.upload to views.checkoutFiles i'm using this command

return redirect(reverse('checkoutFiles', kwargs={'ACCEPTED_FILES':ACCEPTED_FILES}))

...

def checkFiles(request, ACCEPTED_FILES):
  print ACCEPTED_FILES
  return render(request, 'confirm_files.html', {
      'ACCEPTED_FILES': ACCEPTED_FILES
  })

and im getting this error message

NoReverseMatch: Reverse for 'checkoutFiles' with keyword arguments '{'ACCEPTED_FILES': [[u't2_r0Oqwl7.txt', '0.98 KB', u'text/plain']]}' not found. 1 pattern(s) tried: ['checkFiles/']

django version: 1.11.2

Panagiotis Simakis
  • 1,245
  • 1
  • 18
  • 45

2 Answers2

0

When you call reverse, Django follows what you have in urls.py. In there you don't have any parameters specified in the regular expression for checkoutFiles. For example:

url(r'^articles/([0-9]{4})/$', views.year_archive, name='year-archive'),

In this example you can call:

reverse('year-archive', args=[datetime.datetime.today().year])

https://docs.djangoproject.com/en/1.11/topics/http/urls/#example

In your code you must specify what are the parameters you'll receive in that URL.

url(r'^checkFiles/(?P<extension>[\w]+)/$', views.checkFiles, name='checkoutFiles'),

Although it would not be a good idea doing this kind of validation through a URL.

Edit #2

If you want to have something in the URL, you can use a GET parameter:

Whenever you redirect you can do something like this:

return redirect(reverse('checkoutFiles') + '?files={}'.format(ACCEPTED_FILES))

In the view where you are redirected to, you can get the values with

request.GET.get('files', '')

https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.GET

Ev.
  • 1,537
  • 1
  • 24
  • 49
  • still having the same error... i'm trying to display the values of the list 'ACCEPTED_FILES' on a different url.. How would you suggest me do it? thanks in advance – Panagiotis Simakis Jun 13 '17 at 11:39
  • 1
    You can use a `GET` parameter. Whenever you redirect you can do something like this: `return redirect(reverse('checkoutFiles') + '?files={}'.format(ACCEPTED_FILES))`. In the view where you are redirected to, you can get the values with `request.GET.get('files', '')`. – Ev. Jun 13 '17 at 11:47
  • 1
    And you are getting the error because your view has a different variable name. Instead of `extension` use `ACCEPTED_FILES`: `url(r'^checkFiles/(?P[\w]+)/$', views.checkFiles, name='checkoutFiles'),` – Ev. Jun 13 '17 at 11:50
  • i used the GET option and works fine! does it worth to debug or it's better to use a GET parameter? which one is better solution (and if you have time, explain why :P )? thanks for your time! – Panagiotis Simakis Jun 13 '17 at 12:00
  • What does accepted files mean? Is it the name of the files uploaded in another view? Their extensions? I still haven't understood what you want to do with this logic. I mean it is a bad idea because the user can modify the GET parameters manually and you might not be able to check if the values are true or not. And remember to mark the question as correct if it solved the error you had. :) – Ev. Jun 13 '17 at 12:25
  • accepted files is a list of filenames where uploaded by the user in previous step – Panagiotis Simakis Jun 13 '17 at 12:28
  • That's dangerous. Given the following situation: User uploads file_1, file_2 and file_3. You accept only file_1 and file_2. However, the user intentionally adds file_3 in the GET URL, because he can do that. You'll accept all three files even though that was not the idea. I would suggest using another function to do this validation, which would accept the request (among other things) as parameter. From this function you can redirect to wherever you want and still do the validation, without touching the URL. – Ev. Jun 13 '17 at 12:35
-1

You should add a namespace in you project urls.py file and also in below code
Example: project_name/urls.py

url(r'^appname/', include('appname.urls', namespace='namespace')),

views.py

 return redirect(reverse('namespace:checkoutFiles', kwargs={'ACCEPTED_FILES':ACCEPTED_FILES}))
Amar
  • 666
  • 5
  • 13