Django URL conf错误

时间:2016-11-30 09:13:17

标签: django django-views django-urls django-class-based-views

当我尝试使用reverse或reverse_lazy时,我有以下消息:

  

包含的URLconf'ph.urls'似乎没有任何模式   它。如果您在文件中看到有效的模式,则可能是问题所在   由循环导入引起的

当我使用网址时(不使用反向或反向延迟)工作没有问题。我尝试了不同的网址组合相同的结果

我做错了什么?

ph是项目。在项目中,我包含了应用程序的URL:

from django.conf import settings
from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [

    url(r'^account/', include('accounts.urls', namespace='accounts')),
    url(r'^companies/', include('companies.urls', namespace='companies')),
    url(r'^products/', include('products.urls', namespace='products')),
    url(r'^admin/', admin.site.urls),

]

# debug toolbar
if settings.DEBUG:
    import debug_toolbar
    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]

涉及的应用程序:

1)账户:

from django.conf.urls import url
from .views import AccountDetailView

urlpatterns = [
     url(r'$', AccountDetailView.as_view(), name='dashboard'),

2)产品:

class ProductCreateView(AccountMixin, CreateView):
    model = Product
    form_class = ProductModelForm
    url = reverse_lazy('accounts:dashboard')
    url2 = reverse_lazy('products:list')
    #print(url)
    template_name = 'accounts/product_form.html'
    #success_url = reverse_lazy('accounts:product_detail')

1 个答案:

答案 0 :(得分:0)

我解决了这个问题;

我使用get_success_url()调用reverse()。

reverse_lazy没有用,因为有必要将kwargs传递给url,我忘记了。

def get_success_url(self):     返回反向(' accounts:product_detail',kwargs = {' pk':self.object.pk})