Inconsistent URL error in Django from following along to Beginner YT tutorial

As you can see in the first screenshot, /products/new isn't showing up as a valid URL although I followed the coding tutorial from YouTube exactly. For some reason there's a blank character before "new" but no blank space in the current path I'm trying to request. I don't know if that's normal or not. I'm using django version 2.1 if that matters

The URL does work for products/salt/. What's weird is the URL used to be products/trending/ but I got the same error so I randomly changed the URL to salt and it started working for me.

[Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/products/new/ Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:

admin/ products/ products/ salt products/ new The current path, products/new/, didn't match any of these.]1

from django.http import HttpResponse
from django.shortcuts import render


def index(request):
    return HttpResponse('Hello World')


def trending(request):
    return HttpResponse('Trending Products')



def new(request):
    return HttpResponse('New Products')[2]

from django.urls import path
from . import views


urlpatterns = [
    path('', views.index),
    path('salt', views.trending),
    path('new', views.new)[3]

Add a trailing slash / to your URLpatterns to resolve this issue i.e. new/ and trending/.

Also as mentioned in my comment, I would suggest you upgrade to a secure version of Django to access newer features.

Вернуться на верх