4

我想success_url在表单提交并有效后将其重定向到相同的表单。在网上我找到了一个解决方案的描述,但是这个解决方案和我的其他代码选项会生成相同的错误消息。

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

网址.py

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

第一个views.py 选项

复制现有的链接路径

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

第二个views.py选项

根据反向 url 加上 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)})

两个选项都以相同的回溯结束。

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'
4

1 回答 1

10

看起来您的两个get_success_url实现都没有实际返回 URL,因此假设这不仅仅是一个剪切和粘贴错误,您将 None 传递给 HttpResponseRedirect。

于 2013-05-10T20:14:33.410 回答