包含的urlconf xxxx.urls中没有任何模式

时间:2013-07-17 08:47:28

标签: python django

我想在一个modelform类中获取一个url。 我见过 The included urlconf manager.urls doesn't have any patterns in it。但 reverse_lazy 功能对我的情况不起作用。

我的情况:

captchahelper root 中的应用

root urlpatterns(根项目下的urls.py):

urlpatterns = patterns("",
    .....
    # captcha
    url(r'^captcha/', include('captchahelper.urls')),
    .....
}

captcha urlpatterns(在captchahelper项目下的urls.py):

urlpatterns = patterns('',
    url(r'^$', views.captcha , name="views_captcha"),
    url(r'^refresh/$', views.refresh , name="views_refresh_captcha"),
)


captchahelper项目下的 view.py

def captcha(request):
    .....
    return HttpResponse(captcha.gen_img_by_code(code),'image/jpeg')

得到:

class CaptchaForm(forms.ModelForm):
    .....
    captcha = Captcha()
    encoded = captcha.get_encrypt_code()
    captcha_image = urlresolvers.reverse_lazy('views_captcha') + '?encoded='+encoded
    .....
    def clean_captcha_text(self):
        ....


结果错误:
/ account / signup /是当前url(也包含在root.urls中),当访问者查看此页面时,上面的类将在相应的视图函数中初始化。

ImproperlyConfigured at /account/signup/
The included urlconf root.urls doesn't have any patterns in it


D:\Python27\lib\site-packages\django\core\urlresolvers.py in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)) ...
 ▼ Local vars
    Variable    Value
    ns_pattern  
    ''
    viewname    
    'views_captcha'
    args    
    []
    current_app 
    None
    prefix  
    u'/'
    parts   
    ['views_captcha']
    resolver    
    <RegexURLResolver root.urls (None:None) ^/>
    kwargs  
    {}
    path    
    []
    resolved_path   
    []
    urlconf 
    'root.urls'
    view    
    'views_captcha'

我觉得django是如此复杂,因为我来自ASP MVC3.I可以从MVC3中的Url.Action("CaptchaController", "CaptchaView", new { encoded = "encoded"})语句中轻松生成带有查询字符串的URL。但是如果我想在django中生成带有查询字符串的url ,代码将是reverse('captcha') + '?encoded=' + encoded。这真的很难看......

2 个答案:

答案 0 :(得分:0)

您正在调用类,而不是urls文件中的def方法本身。你的项目看起来有点奇怪,因为大多数文件的结尾都是*.py,但这里是我已经使用的项目的一个例子:

urls.py
...
url(r'^getCustomerData/$', views.getCustomerData),
...

并在`views.py,

from app.models import Customer

def getCustomerData(request):
//process and return

所以对你来说,我建议你改变你的网址解析器

url(r'^$', views.captcha , name="views_captcha"),
url(r'^refresh/$', views.refresh , name="views_refresh_captcha")

url(r'^$', views.captcha.CAPTCHA_HANDLER_DEF , name="views_captcha"),
url(r'^refresh/$', views.captcha.refresh , name="views_refresh_captcha")

并在验证码中定义这两个函数。

答案 1 :(得分:0)

确保您的urls.py文件中包含urlpatterns且拼写正确。

urlpatterns = [
    url(r'^$', views.article_list),
]