path('index/api-root/', include((router.urls, 'api'), namespace='api-root')),
path('index/' , home, name='home'),
path('about/' , about, name='about'),
我的路由器
router.register('bankdetailapi', views.BankViewSet, basename='bankdetail' )
我是如何在模板写网址
href="{% url 'about' %}"
href="{% url 'api-root' %}"
href="{% url 'home' %}"
我得到以下错误
Traceback (most recent call last):
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\rest_api\api\views.py", line 36, in home
return render(request,'index.html')
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\template\defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\urls\base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\Users\JAY SARDAR\projects\Django+Angular\Fyle-assignment\Backend\restapi\lib\site-packages\django\urls\resolvers.py", line 694, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /index/
Exception Value: Reverse for 'api-root' not found. 'api-root' is not a valid view function or pattern name.
当我把API的url改为硬编码的http链接时,一切都很好。我认为写路由器url的语法是不同的。还有如何写Basename参数的href?
2条答案
按热度按时间gopyfrb31#
显然,这是非常容易的。当你包括路由器时,你必须使用
"{% url 'app_name:namespace' %}"
才能让它工作。但是我仍然找不到如何使用url作为basename参数。moiiocjp2#
解决了我的问题。它是如何工作的?经过几次反复试验,我发现这个是有效的: