Django-grappelli管理员:没有反向匹配错误 [英] Django-grappelli admin: No reverse match error
问题描述
我已经在一个django项目上工作了一段时间,现在使用grappelli作为管理员,突然之间,我的change_form.html模板正在抛出以下错误:
I've been working on a django project for a while now that uses grappelli for the admin and all of a sudden today my change_form.html template is throwing the following error:
Caught NoReverseMatch while rendering: Reverse for "grp_related_lookup" with arguments '()' and keyword arguments '{}' not found.
违规代码行是第38行:
The offending line of code is line 38:
37 $.each(related_lookup_fields_fk, function() {
38 $("#id_" + this).grp_related_fk({lookup_url:"{% url grp_related_lookup %}"});
39 });
之前是这一段代码:
var related_lookup_fields_fk = {% get_related_lookup_fields_fk adminform.model_admin %};
显然这是 {%url grp_related_lookup%}
我不明白模板如何解决 grp_related_lookup
到 grappelli.views.related.related_lookup
。我已经尝试用 grappelli.views.related.related_lookup
替换 grp_related_lookup
,这也没有。此外,在模板中,违规行如下所示:
I don't understand how the template is resolving grp_related_lookup
to grappelli.views.related.related_lookup
. I have tried replacing grp_related_lookup
with grappelli.views.related.related_lookup
and that didn't work either. Also, in the template the offending line looks like this:
$("#id_" + this).grp_related_fk({lookup_url:"{% url grp_related_lookup %}"});
但在错误信息中如下所示:
but in the error message it looks like this:
$("#id_" + this).grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
我不知道围绕 grp_related_lookup
可能与问题有关。 django如何呈现函数调用?是否将字符串'grp_related_lookup'
传递给url模板标签?如果是这样,可能会导致这样做突破?
I don't know if the single quotes surrounding grp_related_lookup
might have something to do with the problem or not. Is that how django rendered the function call? Is it passing the string 'grp_related_lookup'
to the url template tag? If so, what might have caused this to break suddenly?
一些额外的信息:
-
related_lookup_fields
的值为空列表[]
。我没有在我的admin.py中定义任何related_lookup_fields
。 - 我将一些调试语句投入到
grappelli.views.related.related_lookup
查看功能,似乎没有被调用。 - 我最近没有碰到任何模板。 li>
- The value of
related_lookup_fields
is an empty list[]
. I am not defining anyrelated_lookup_fields
in my admin.py. - I threw a couple debug statements into the
grappelli.views.related.related_lookup
view function and it doesn't appear to be getting called. - I have not touched any of the templates recently.
希望有人可以指向正确的方向...谢谢!
Hopefully someone can point me in the right direction... Thanks!
推荐答案
您的URLconf中是否包含'grappelli.urls'
?这是唯一的原因,我会看到这将导致这个错误。您可以尝试使用django中的 python manage.py shell
:
Do you still have 'grappelli.urls'
included in your URLconf? That the only reason I see that would cause this error. You can try using python manage.py shell
:
from django.core.urlresolvers import reverse
print reverse('grp_related_lookup')
如果此行返回正确的URL,你不应该在你的模板中得到一个 NoReverseMatch
。
If this line returns the correct URL, you shouldn't get a NoReverseMatch
in your template.
grp_related_lookup
不应该是一个问题。 {%url%}
标签接受引用和未引用的字符串作为第一个参数,因此django将其标准化为引用的字符串。这种行为将来会发生变化:您可以使用无引号的字符串将模板变量传递给 {%url%}
。 {%url foo%}
和 {%urlfoo%}
将不会产生相同的结果,有关详细信息的 1.3发行说明关于这个。
The quotes around grp_related_lookup
shouldn't be a concern. The {% url %}
tag accepts both quoted and unquoted strings as first argument, so django normalizes it to quoted strings. This behaviour is going to change in the future: you'll be able to pass template variables to {% url %}
using unquoted strings. {% url foo %}
and {% url "foo" %}
won't give the same result, see the 1.3 release notes for details about this.
这篇关于Django-grappelli管理员:没有反向匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!