我正在尝试根据API的模型数据自动生成一堆类似的URL和视图。我从模型生成一个树,并验证它是正确的。但是,当我尝试使用该树在嵌套命名空间中构建嵌套URL时,会构建URL,但命名空间不是。
相关代码:
# urls.py
urlpatterns = patterns('',
url(r'^$', views.Index.as_view(), name='index'),
url(r'^api/', include(rest_api.generate_urls('api'),
namespace='api', app_name='app')),
)
# rest_api.py
def build_urls(tree, namespaces):
# Create the response function, which relies on data from tree
if condition1:
def response(request, **kwargs):
...
return build_response(data_from_tree)
elif condition2:
def response(request, **kwargs):
...
return build_response(data_from_tree)
else:
def response(request, **kwargs):
...
return build_not_found_response(data_from_tree)
urls = []
# response will present unique identifier, URL for each entry in Model
urls.append(url(r'^$', response, name='index'))
# Add any sub-paths before open-ended regex.
for path, node in tree.items():
urls.append(url(r'^{path}/'.format(path=path),
include(build_urls(node, namespaces + (path,)),
namespace=path, app_name='app')))
# response will query Model for appropriate entry & present data
urls.append(url(r"^(?P<name>[\w ']+)", response))
return patterns('', *urls)
def generate_urls(*namespaces):
return build_urls(tree, namespaces)
当我查看生成的URLconf(来自django-extensions的python manage.py show_urls
)时,没有嵌套的命名空间(为便于阅读而清理):
[riggs@dev app]$ python manage.py show_urls
/app/ app.views.Index index
/app/api/ app.rest_api.response api:index
/app/api/equipment/ app.rest_api.response api:index
/app/api/equipment/<name> app.rest_api.response
/app/api/materials/ app.rest_api.response api:index
/app/api/materials/<name> app.rest_api.response
/app/api/materials/raw/ app.rest_api.response api:index
/app/api/materials/raw/<name> app.rest_api.response
/app/api/materials/output1/ app.rest_api.response api:index
/app/api/materials/output1/<name> app.rest_api.response
/app/api/materials/output2/ app.rest_api.response api:index
/app/api/materials/output2/<name> app.rest_api.response
/app/api/recipe/ app.rest_api.response api:index
/app/api/recipe/stage1/ app.rest_api.response api:index
/app/api/recipe/stage1/<name> app.rest_api.response
/app/api/recipe/stage2/ app.rest_api.response api:index
/app/api/recipe/stage2/<name> app.rest_api.response
我不明白为什么这不起作用。
最终,我想要这些命名视图,以便我可以通过django.core.urlresolvers.reverse
在网址中引用它们。尽管缺乏嵌套,我还是可以通过更改
reverse
urls.append(url(r'^$', response, name='index'))
到
urls.append(url(r'^$', response, name=namespaces[-1]))
将api:index
变为api:equipment
,api:materials
,api:raw
等。但是,当我使用
def response(request, **kwargs):
url = request.build_absolute_uri(
reverse(':'.join(('api', viewname)),
current_app=request.resolver_match.namespace))
...
return build_response(data_from_tree)
Django抛出NoReverseMatch
消息Reverse for 'equipment' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
。
我做错了什么? django真的有能力做我想做的事吗,在这里?
答案 0 :(得分:0)
原来上面的作品很棒! show_urls
不处理名称空间,因此我假设名称空间错误会掩盖不同的错误。一旦我信任他们是正确的,我就能找到有问题的代码。