如何编辑urls.py?
how to edit urls.py?
我需要使用此路径 graphs/<direction>
创建 url。 direction
应该是一个字符串,因为它是某个部门的名称。
我试过这种格式,但是没用。它告诉我:
Page not found (404)
Request Method: GET
Request 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
)
和我的graphs.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()),
]
我需要使用此路径 graphs/<direction>
创建 url。 direction
应该是一个字符串,因为它是某个部门的名称。
我试过这种格式,但是没用。它告诉我:
Page not found (404) Request Method: GET Request 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
)
和我的graphs.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()),
]