我正在尝试设置网络应用程序。我在将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.朋友
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.朋友
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})