Home > Software design >  how to edit urls.py?
how to edit urls.py?

Time:04-30

i need to create url with this path graphs/<direction>. The direction should be a string because it's the name of some department.

i've tried to do this kind of formating, but it didn't work. It tells me that:

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
        )

and my graphs.html

{% extends "vm_mon/wrapper.html" %}

{% block content %}

    <div >

        <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 %}

CodePudding user response:

you are missing the 'graphs' in the url:

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