【问题标题】:No module named urls in django-tastypiedjango-tastypie 中没有名为 urls 的模块
【发布时间】:2012-05-18 15:46:34
【问题描述】:

我只是复制粘贴tastepie 示例代码来了解它是如何工作的。代码如下。 我也做了模型类条目。当我在 url 上运行 http://localhost:8000/api/v1/ 时会抛出错误

# myapp/api/resources.py
from django.contrib.auth.models import User
from tastypie.authorization import Authorization
from tastypie import fields
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
from myapp.models import Entry


class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
        filtering = {
        'username': ALL,
    }


class EntryResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')

class Meta:
    queryset = Entry.objects.all()
    resource_name = 'entry'
    authorization = Authorization()
    filtering = {
        'user': ALL_WITH_RELATIONS,
        'pub_date': ['exact', 'lt', 'lte', 'gte', 'gt'],
    }

urls.py

from django.conf.urls.defaults import *
from tastypie.api import Api
from myapp.api.resources import EntryResource, UserResource

v1_api = Api(api_name='v1')
v1_api.register(UserResource())
v1_api.register(EntryResource())

urlpatterns = patterns('',
    # The normal jazz here...
    (r'^blog/', include('myapp.urls')),
    (r'^api/', include(v1_api.urls)),
 )

它正在抛出消息“没有名为 urls 的模块”。有什么想法吗?

【问题讨论】:

  • 您需要包含回溯。甚至在哪里引发此错误都模棱两可。我猜是v1_api.urls?
  • 您在“include(v1_api.urls)”中遗漏了引号
  • 您的错误行号显示在哪里?

标签: python django rest tastypie


【解决方案1】:

你应该试试:

(r'^api/', include('v1_api.urls')),

【讨论】:

    【解决方案2】:

    这个错误是因为在 myapp 包中没有名为 urls.py 的模块。在 myapp 包中创建一个模块 urls.py

    【讨论】:

      【解决方案3】:

      代替

      from django.conf.urls.defaults import *

      你可以试试这样导入

      from django.conf.urls import *
      

      【讨论】:

        猜你喜欢
        • 2015-09-13
        • 2016-06-15
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        • 2012-06-26
        • 2014-05-16
        • 2017-06-04
        相关资源
        最近更新 更多