如何编辑urls.py?



我需要用这个路径graphs/<direction>创建url。direction应该是字符串,因为它是某个部门的名称。

我试过做这种格式化,但它没有工作。它告诉我:

页面未找到(404)请求方法:GET请求URL: http://127.0.0.1:8000/graphs/PS/

urls . py

from django.urls import path
from . import views
urlpatterns = [
path('<str:direction>', views.ViewDashboard.as_view()),
]

views.py

class ViewDashboard(View):
def get(self, request, direction: str):

string_query = request.get_full_path().split(f"{direction}/")[1]
entry = Department.objects.get(department__exact=direction)
data = {
"title": f"Графики для направления: {direction}",
"level_navigation": {
"level": 2,
"name_level_1": "vm",
"url_level_1": "",
},
"offset": eval(entry.info_grafana),
"grafana_ip": grafana['ip_grafana_server'],
"https": grafana['https'] if grafana.get('https') else 'http',
"from": "now-30d",
}
if string_query:
string_query = string_query[1:].split("&")
for query in string_query:
if query.startswith('from='):
data['from'] = query.split('from=')[1]
return render(
request, 
template_name='graphs/graphs.html', 
context=data
)

and mygraphs.html

{% extends "vm_mon/wrapper.html" %}
{% block content %}
<div class="container">
<iframe src="{{ https }}://{{ grafana_ip }}:3000/d/{{ offset.uid }}/{{ offset.slug }}?orgId=1&from=now-30d&to=now&refresh=1d" width="100%" height="1600" frameborder="0"></iframe>
</div>
{% endblock %}

你漏掉了url中的" graphs ":

urlpatterns = [
path('graphs/<str:direction>/', views.ViewDashboard.as_view()),
]

最新更新