【问题标题】:Reverse for 'about_abc' with arguments '('',)' not found未找到带有参数“(”,)”的“about_abc”的反向
【发布时间】:2019-09-14 17:35:42
【问题描述】:

我正在尝试设置网络应用程序。我在将 host_id 传递给模板 .html 文件时遇到问题

我明白了:

Reverse for 'about_abc' with arguments '('',)' not found. 1 pattern(s) tried: ['itpassed\\/(?P<host_id>[0-9]+)\\/about\\/$']

inter.html

<li><a href="{% url 'about_abc' host_id %}">about</a></li>

当我使用“1”而不是“host_id”时,它可以工作,但不能像这样保持硬编码。

views.py

from django.shortcuts import render
import warnings
import requests
import json
from django.http import HttpResponse
from django.template import loader
from .models import Host
[...]

def inter(request, host_id):
    return render(request, 'itpassed/inter.html')

def about_abc(request, host_id):
    response = requests.get(
        'abc.net:1768/abc/api/v1/about',
        verify='/cert/cacerts.pem',
        headers={'Accept': 'application/json', 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxx'},
    )
    return HttpResponse(response.content)

urls.py

from django.urls import path
from .models import Host
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:host_id>/', views.inter, name='inter'),
    path('<int:host_id>/about/', views.about_abc, name='about_abc'),
]

如何解决这个问题?据我所见,views.py 应该将 host_id 传递给模板。 为什么硬编码的“1”有效,但host_id 无效? 谢谢

【问题讨论】:

    标签: python django django-templates django-views django-urls


    【解决方案1】:
    def inter(request, host_id):
        return render(request, 'itpassed/inter.html')
    

    您没有在此处向inter.html 模板传递任何内容 - 因此host_id 模板变量不会保存任何值。

    我相信你想要这个:

    def inter(request, host_id):
        return render(request, 'itpassed/inter.html', {"host_id": host_id})
    

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 2021-03-01
      • 2021-03-13
      • 2019-04-21
      • 2020-05-11
      • 2010-12-22
      • 2013-12-07
      • 2021-04-13
      相关资源
      最近更新 更多