Home > Enterprise >  Current path didn't match the url patterns - Django
Current path didn't match the url patterns - Django

Time:10-30

I am unable to get the url redirected to one of the patterns defined in the url routing file.

web.py

from django.urls import path
from django.contrib import admin

from heyurl import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('store', views.store, name='store'),
    path('/metrics/', views.get_month_metrics, name= 'metrics'),
]

views.py (Includes the function that is getting called along with the libraries included)

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Url, Click

from django.core.validators import URLValidator, ValidationError
from heyurl.utils import db_services, helper
from django_user_agents.utils import get_user_agent
from django.template.defaulttags import register
from datetime import datetime


def get_month_metrics(request, url):
    today = datetime.now()
    ident = Url.objects.filter(short_url= url)
    ident = ident[0].id

# GETTING THE CLICKS THIS MONTH
clicks_this_month = Click.objects.filter(url=ident, created_at__year=today.year,
                                         created_at__month=today.month)

# TOTAL CLICKS PER BROWSER
safari = Click.objects.filter(url=ident, browser__contains='safari')
chrome = Click.objects.filter(url=ident, browser__contains='chrome')
firefox = Click.objects.filter(url=ident, browser__contains='firefox')


# TOTAL CLICKS PER PLATFORM
mobile = Click.objects.filter(url=ident, platform='Mobile')
pc = Click.objects.filter(url=ident, platform='PC')

#CONTEXT TO DISPLAY ON DATA PANEL
context = {
    'url': url,
    'clicks': len(clicks_this_month),
    'safari': len(safari),
    'chrome': len(chrome),
    'firefox': len(firefox),
    'mobile': len(mobile),
    'pc': len(pc),
}

return render(request, 'heyurl/metrics.html', context)

Now I tried hardcoding and supplied the exact pattern that it says is missing by changing the web.py as follows

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('store', views.store, name='store'),
    path('RQvw4/metrics/', views.get_month_metrics, name= 'metrics'),
]

and it gives me the following error error after hardcoding url

CodePudding user response:

I think you expect to get RQvw4 as url - so you should define path with parameter <url> like

path('<url>/metrics/', ...)

And then you should keep url in function

def get_month_metrics(request, url):

EDIT:

If you hardcode

path('RQvw4/metrics/',...)

then it doesn't know that you want to assign RQvw4 to url.
It would need to hardcode this value also in function

def get_month_metrics(request, url="RQvw4"):

But it still would need to use path('<url>/metrics/', ...) for other urls.

CodePudding user response:

Remove url argument from get_month_metrics() and access url using request.path. It should look like this:

def get_month_metrics(request):
    url = request.path
    # rest of your code
  • Related