0

在我的模板、模型等中,所有其他反向 url(编辑、删除等)似乎都有效,但在我的业务应用程序views.py中却没有这个(错误跳下):

from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from django.core.urlresolvers import reverse

from business.models import Country

{...}

# Delete
class CountryDeleteView(DeleteView):
model = Country
template_name_suffix = '_delete_form'
success_url = reverse('country_listview')  # commenting this out makes everything work

项目 urls.py:

from django.conf.urls import patterns, include, url

urlpatterns += patterns('',
    url(r'^business/', include('business.urls')),
    )

业务应用程序 urls.py:

from django.conf.urls import patterns, url
from business.views import CountryListView, CountryDetailView
from business.views import CountryCreateView, CountryUpdateView, CountryDeleteView


urlpatterns = patterns('',
    url(r'^country/$', CountryListView.as_view(), name='country_listview'),
    url(r'^country/(?P<pk>\d+)/$', CountryDetailView.as_view(), name='country_detailview'),
    url(r'^country/create/$', CountryCreateView.as_view(), name='country_createview'),
    url(r'^country/(?P<pk>\d+)/update/$', CountryUpdateView.as_view(), name='country_updateview'),
    url(r'^country/(?P<pk>\d+)/delete/$', CountryDeleteView.as_view(), name='country_deleteview'),
    )

我使用model_listview而不是仅仅model_list因为默认情况下ListView通用编辑视图已经model_list在上下文中传递(我没有在我的ListView子类中指定我应该为我的上下文变量命名的内容)并且它与模板country_list.html中的此代码冲突:

<ul>
    {% for c in country_list %}
        <li>{{ c.name }}<br>
            <a href="{% url country_detailview c.pk %}">Detalii</a>
            <a href="{% url country_updateview c.pk %}">Modifica</a>
            <a href="{% url country_deleteview c.pk %}">Sterge</a>
        </li>
    {% endfor %}
</ul>

和错误:

NoReverseMatch 在 /business/country/

未找到带有参数“()”和关键字参数“{}”的“country_listview”的反向操作。

请求方法:GET

请求网址:_removed_ip_:8000/business/country/

Django 版本:1.4.3

异常类型:NoReverseMatch

异常值:

未找到带有参数“()”和关键字参数“{}”的“country_listview”的反向操作。

异常位置:/usr/lib/python2.7/site-packages/django/core/urlresolvers.py 在 _reverse_with_prefix,第 396 行

Python 可执行文件:/usr/bin/python

Python版本:2.7.3

4

1 回答 1

2

可能是在加载 url 之前定义了表单。试试 reverse_lazy 看看是否可行。

未经测试:

from django.core.urlresolvers import reverse_lazy


...

success_url = reverse_lazy('country_listview')
于 2012-12-23T04:23:05.017 回答