我只是复制粘贴tastypie示例代码以了解它是如何工作的。代码如下。 我也做了modelclass Entry。当我在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的模块”。有什么想法吗?
答案 0 :(得分:0)
你应该尝试:
(r'^api/', include('v1_api.urls')),
答案 1 :(得分:0)
此错误正在发生,因为myapp包中没有名为urls.py的模块。在myapp包中创建一个模块urls.py
答案 2 :(得分:0)
而不是
from django.conf.urls.defaults import *
您可以尝试像这样导入
from django.conf.urls import *