Django:反向函数失败并出现异常
我正在关注 Django 教程,但在教程的第 4 部分遇到了错误。 我已经到了编写vote视图的部分,它使用reverse重定向到另一个视图。 由于某种原因,反向失败,但出现以下异常:
import() 参数 1 必须是字符串,而不是实例方法
目前我的项目的 urls.py 看起来像这样:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/(.*)', include(admin.site.root)),
)
应用程序 urls.py 是:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'details'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
投票视图是:(我已将其简化为仅包含错误的行)
def vote(request, poll_id):
return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(1,)))
当我从项目的 urls.py 中删除管理 urls 时,即使其变为:
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
#(r'^admin/(.*)', include(admin.site.root)),
)
它有效。
我尝试了很多事情,但无法理解我做错了什么。
I'm following the Django tutorial and got stuck with an error at part 4 of the tutorial. I got to the part where I'm writing the vote view, which uses reverse to redirect to another view. For some reason, reverse fails with the following exception:
import() argument 1 must be string, not instancemethod
Currently my project's urls.py looks like this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/(.*)', include(admin.site.root)),
)
and the app urls.py is:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^
And the vote view is: (I've simplified it to have only the row with the error)
def vote(request, poll_id):
return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(1,)))
When I remove the admin urls include from the project's urls.py, i.e. making it into:
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
#(r'^admin/(.*)', include(admin.site.root)),
)
it works.
I've tried so many things and can't understand what I'm doing wrong.
, 'index'),
(r'^(?P<poll_id>\d+)/
And the vote view is: (I've simplified it to have only the row with the error)
When I remove the admin urls include from the project's urls.py, i.e. making it into:
it works.
I've tried so many things and can't understand what I'm doing wrong.
, 'details'),
(r'^(?P<poll_id>\d+)/results/
And the vote view is: (I've simplified it to have only the row with the error)
When I remove the admin urls include from the project's urls.py, i.e. making it into:
it works.
I've tried so many things and can't understand what I'm doing wrong.
, 'results'),
(r'^(?P<poll_id>\d+)/vote/
And the vote view is: (I've simplified it to have only the row with the error)
When I remove the admin urls include from the project's urls.py, i.e. making it into:
it works.
I've tried so many things and can't understand what I'm doing wrong.
, 'vote'),
)
And the vote view is: (I've simplified it to have only the row with the error)
When I remove the admin urls include from the project's urls.py, i.e. making it into:
it works.
I've tried so many things and can't understand what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在过去的几个版本中,包含管理 URL 的方式已经发生了几次变化。 您可能对已安装的 Django 版本使用了错误的说明。
如果您使用当前的主干 - 即不是官方版本 - 那么文档位于 http://docs .djangoproject.com/en/dev/ 是正确的。
但是,如果您使用的是 1.0.2,那么您应该按照页面顶部的链接访问 http ://docs.djangoproject.com/en/1.0/。
The way you include the admin URLs has changed a few times over the last couple of versions. It's likely that you are using the wrong instructions for the version of Django you have installed.
If you are using the current trunk - ie not an official release - then the documentation at http://docs.djangoproject.com/en/dev/ is correct.
However, if you are using 1.0.2 then you should follow the link at the top of the page to http://docs.djangoproject.com/en/1.0/.