如何在django模板中使用路由器为基于类的视图指定动态url?

mqkwyuun  于 2022-12-20  发布在  Go
关注(0)|答案(2)|浏览(115)
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?

gopyfrb3

gopyfrb31#

href="{% url 'api:api-root' %}"

显然,这是非常容易的。当你包括路由器时,你必须使用"{% url 'app_name:namespace' %}"才能让它工作。但是我仍然找不到如何使用url作为basename参数。

moiiocjp

moiiocjp2#

"{% url 'api:api-root' %}bankdetailapi/?q=">

解决了我的问题。它是如何工作的?经过几次反复试验,我发现这个是有效的:

"{% url 'app_name:namespace'%}basename/query_parameter"

相关问题