我在django中为一个模型创建了资源,并在urls.py中注册 但是当我访问主页后出现错误。
错误堆栈:
ERROR: Internal server error: /
Traceback (most recent call last):
File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 98, in get_response
resolver_match = resolver.resolve(request.path_info)
File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 338, in resolve
for pattern in self.url_patterns:
File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 367, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 361, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/himanshu_office/projects/NDSE/NDSE/urls.py", line 28, in <module>
url(r'^questionnaire/', include('questionnaire.urls', namespace='questionnaire')),
File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 28, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/himanshu_office/projects/NDSE/NDSE/questionnaire/urls.py", line 8, in <module>
questionnaire_api_v1.register(questionnaire_resources.SectionResource)
File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/tastypie/api.py", line 54, in register
resource.__class__.Meta.api_name = self.api_name
AttributeError: type object 'ModelDeclarativeMetaclass' has no attribute 'Meta'
file:questionnaire / api / resources.py
from tastypie import resources, fields
from tastypie.authorization import Authorization
from questionnaire import models as questionnaire_models
class SectionResource(resources.ModelResource):
"""
Resource for Section model with deepest level
"""
class Meta:
resource_name = 'section'
queryset = questionnaire_models.Section.objects.all()
list_allowed_methods = ['get']
detail_allowed_methods = ['get']
authorization = Authorization()
always_return_data = True
include_resource_uri = True
fields = ['name']
file:questionnaire / models.py
from jsonfield import JSONField
from sanitizer.models import SanitizedTextField
from django.db import models
from evaluation import models as evaluation_models
class Section(models.Model):
"""
Model to Store Section which represent collection of questions
"""
name = models.CharField(max_length=100, unique=True,
help_text="Unique name of section to classify questions")
def __unicode__(self):
return self.name
file:questionnaire / urls.py
from django.conf.urls import patterns, url, include
from tastypie.api import Api
from questionnaire.api import resources as questionnaire_resources
questionnaire_api_v1 = Api(api_name='questionnaire')
questionnaire_api_v1.register(questionnaire_resources.SectionResource)
urlpatterns = patterns('',
url('^', include(questionnaire_api_v1.urls)),
)
答案 0 :(得分:4)
我在注册api时犯了错误 它应该是
questionnaire_api_v1.register(questionnaire_resources.SectionResource())
取代:
questionnaire_api_v1.register(questionnaire_resources.SectionResource)