Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: dynamic success_url results in "'NoneType' object has no attribute 'find'"

I would like to redirect the success_url to the same form after the form was submitted and valid. Online I found a description of a solution, however this solution and my other code option generate the same error message.

Exception Value: 'NoneType' object has no attribute 'find'

urls.py

url(r'^pictures/(?P<id>\d+)/$', PictureUpload.as_view(), name='picture_upload'),
...

1st views.py option

copies the existing link path

class PictureUpload(FormView):
    form_class = PictureForm
    template_name = 'picture_upload.html'   
    def get_success_url(self):
        success_url = lambda self: self.request.path

2nd views.py option

recreates the link path based on the reverse url plus the id

class PictureUpload(FormView):
    form_class = PictureForm
    template_name = 'picture_upload.html'   
    def get_success_url(self):
        success_url = reverse('picture_upload', 
            kwargs={'id': self.kwargs.get('id', None)})

Both options end up in the same traceback.

Request Method: POST
Request URL: http://127.0.0.1:8000/pictures/2/

Django Version: 1.5.1
Python Version: 2.7.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.gis',
 'django.contrib.humanize',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'picture',
 'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  165.             return self.form_valid(form)
File "/Users/Development/project/apps/picture/picture_views.py" in form_valid
  180.         return super(PictureUpload, self).form_valid(form)
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/views/generic/edit.py" in form_valid
  65.         return HttpResponseRedirect(self.get_success_url())
File "/Users/Development/virtual-re/lib/python2.7/site-packages/django/http/response.py" in __init__
  388.         parsed = urlparse(redirect_to)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urlparse.py" in urlparse
  134.     tuple = urlsplit(url, scheme, allow_fragments)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urlparse.py" in urlsplit
  173.     i = url.find(':')

Exception Type: AttributeError at /pictures/2/
Exception Value: 'NoneType' object has no attribute 'find'
like image 845
neurix Avatar asked Dec 27 '22 04:12

neurix


1 Answers

It looks like neither of your get_success_url implementations actually return the URL, so assuming that's not just a cut-and-paste error you're passing None to the HttpResponseRedirect.

like image 91
Peter DeGlopper Avatar answered Jan 04 '23 15:01

Peter DeGlopper