Django-tastypie错误。我正在尝试prepend_urls以便我可以为用户列出朋友但我收到错误"名称错误在/ api / v1 / friends / user / 1 /全局名称' url'未定义" 。 这是Friends Resource的代码。
class FriendsResource(ModelResource):
from_user=fields.ForeignKey(UserResource,'from_user')
to_user=fields.ForeignKey(UserResource,'to_user')
class Meta:
queryset=Friends.objects.all()
serializer=Serializer(formats=['json'])
resource_name='friends'
filtering={
'from_user':ALL_WITH_RELATIONS,
'to_user':ALL_WITH_RELATIONS
}
这里是prepend_urls的代码和放入wrap_view的方法。
def get_users(self,request):
self.method_check(request,['get'])
friends = []
for friend in Friends.objects.filter(Q(from_user=request.user)|Q(to_user=request.user)):
friends.append(friend)
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/user%s$" %(self._meta.resource_name,trailing_slash()),
self.wrap_view('get_users'),name= 'api_get_friends_for_user')
]
这是Traceback:
Environment:
Request Method: GET
Request URL: http://localhost:8000/api/v1/friends/user/1/
Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tastypie',
'userprof',
'relations',
'event',
'liking',
'feed')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/handlers/base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/utils/importlib.py" in import_module
40. __import__(name)
File "/root/python/django-zack/wyat/wyat/urls.py" in <module>
25. url(r'^api/',include(v1_api.urls)),
File "/root/python/django-zack/local/lib/python2.7/site-packages/tastypie/api.py" in urls
107. pattern_list.append((r"^(?P<api_name>%s)/" % self.api_name, include(self._registry[name].urls)))
File "/root/python/django-zack/local/lib/python2.7/site-packages/tastypie/resources.py" in urls
324. urls = self.prepend_urls()
File "/root/python/django-zack/wyat/event/api.py" in prepend_urls
68. url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/user%s$" %(self._meta.resource_name,trailing_slash()),
Exception Type: NameError at /api/v1/friends/user/1/
Exception Value: global name 'url' is not defined
请告诉我我错在哪里,因为我在嵌套资源上使用了食谱中的例子,我无法看到我错在哪里。
答案 0 :(得分:1)
@Zacmwa
我有一个工作prepend_urls ..你可以看看下面的例子。
def prepend_urls(self): return [ url(r"^(?P%s)/generate%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('genusr'), name="api_get_genusr"), ] def genusr(self, request, **kwargs): data = self.deserialize(request, request.body, format=request.META.get('Content-Type', 'application/json')) print(data.get('workflows',None)) child_resource = UserResource() return child_resource.get_list(request)
以上方法包含在我的资源模型中。
可能由于某些丢失的导入而导致错误,请尝试从tastypie.utils import trailing_slash 导入 django.core.urlresolvers import resolve,然后重试。让我知道你这样做会发生什么。