python - 未找到带有参数“(”,)”的“about_abc”的反向
问题描述
我正在尝试设置网络应用程序。我在将 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”时它可以工作,但它不能像这样保持硬编码。
视图.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)
网址.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 无效?谢谢
解决方案
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})
推荐阅读
- javascript - 如何使用 html、css 和 javascript 创建 Accordion。这样一次打开一个,当打开第一个页面时,默认情况下应该打开一个
- javascript - 如何使用来自 GraphQL 的数据在 Javascript 中创建对象?
- java - 排序链表中的查找方法
- html - 如何定义在渲染时定义的 E.js 文件中使用的变量?
- generics - 在 Haxe 中,您可以编写一个泛型接口,其中方法类型参数受类的类型参数约束吗?
- php - 如何修复数组映射在循环中重复值?
- python - 在python中为子图设置动画
- python - 如何使用python根据列表中出现的父值提取子值?
- python - pandas 使用值的名称创建列并用 True/False 替换
- c - Pulseaudio 的 pa_simple_read() 输出 0-5 或 248-255