Django:反向函数失败并出现异常

发布于 2024-07-26 22:58:24 字数 1199 浏览 10 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ζ澈沫 2024-08-02 22:58:24

在过去的几个版本中,包含管理 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/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文