【问题标题】:Django 1.5 The included urlconf doesn't have any patterns in itDjango 1.5 包含的 urlconf 中没有任何模式
【发布时间】:2013-07-05 23:34:20
【问题描述】:

整合一个 django 应用程序(我的第一个 django/python 任何东西)。我的 application/urls.py 的另一个文件中有一些 url 模式。但是当我启动并尝试导航到任何东西时,我得到 '~/Development/PFM/finances/urls.py' 中包含的 urlconf 模块 'finances.urls' 中没有任何模式

我在另一篇文章中读到,here

视图中的反向查找存在潜在问题。我只是使用通用的基于类的视图和一个自定义视图,所以我不确定从哪里开始。代码如下:

PFM/urls.py:

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

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
                       url(r'^finances/', include('finances.urls')),
                       # Examples:
                       # url(r'^$', 'PFM.views.home', name='home'),
                       # url(r'^PFM/', include('PFM.foo.urls')),

                       # Uncomment the admin/doc line below to enable admin documentation:
                       # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

                       # Uncomment the next line to enable the admin:
                       url(r'^admin/', include(admin.site.urls)),
)

PFM/finances/urls.py:

from django.conf.urls import patterns, url
from finances import views

urlpatterns = patterns
('',
    url(r'^$', views.ListView.as_view(), name='index'),
    url(r'^(P<pk>\d+/$)', views.TransactionList, name='detail'),  # transaction list
    url(r'^/account/(P<pk>\d+)/$', views.AccountList.as_view(), name='detail'),  # account detail
    url('/account/create/', views.account, name='create'),  # account create
    url(r'^/account/update/(P<pk>\d+)/$', views.AccountUpdate.as_view(), name='update'),  # account update
    url(r'^/account/delete/(P<pk>\d+)/$', views.AccountDelete.as_view(), name='delete'),  # account delete
 )

finances/models.py(以备不时之需)

#views for Account
class AccountList(DetailView):
    model = Account
    object_id = Account.pk


class AccountUpdate(UpdateView):
    model = Account
    object_id = Account.pk


class AccountDelete(DeleteView):
    model = Account
    object_id = Account.pk
    post_delete_redirect = "finances/"


#create form/view for Account
def account(request):
    if request.method == 'POST':
        form = AccountForm(request.POST)
        if form.is_valid():
            #save the data?
            Account.save()
            return HttpResponseRedirect('/index.html')
        else:
            form = AccountForm()

        return render(request, 'account_create.html', {
            'form': form,
        })


#views for Transactions
class TransactionList(ListView):
    template_name = "finances/index.html"

    def get_queryset(self):
        return Transaction.objects.order_by('-due_date')

感谢任何帮助。 谢谢

【问题讨论】:

    标签: python django python-2.7 django-views


    【解决方案1】:

    修正语法:

    urlpatterns = patterns('',
        url(r'^$', views.ListView.as_view(), name='index'),
        url(r'^(P<pk>\d+/$)', views.TransactionList, name='detail'),  # transaction list
        url(r'^/account/(P<pk>\d+)/$', views.AccountList.as_view(), name='detail'),  # account detail
        url('/account/create/', views.account, name='create'),  # account create
        url(r'^/account/update/(P<pk>\d+)/$', views.AccountUpdate.as_view(), name='update'),  # account update
        url(r'^/account/delete/(P<pk>\d+)/$', views.AccountDelete.as_view(), name='delete'),  # account delete
     )
    

    patterns 之后的 \n 是问题所在

    【讨论】:

    • 太棒了。谢谢。依赖空白需要一些时间来适应。
    【解决方案2】:

    由于这个问题是第一个出现在 Google 中的问题,所以我想补充一下。

    对我来说,问题是由于django-debug-toolbar 配置不当造成的。 请参阅以下问题:ImproperlyConfigured: The included urlconf <project>.urls doesn't have any patterns in it

    使用 Django 1.6。

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 2011-03-01
      • 2014-11-22
      • 2011-09-22
      • 2013-07-15
      • 2018-10-22
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多