Reputation: 109
I made a project called trydjango where I can access products I created with the products/<int:my_id> [name='product']
link, but somehow it isnt working and I cant find the error.
When i open http://127.0.0.1:8000/products/1/
, i get a 404 page not found error that looks like this
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products/1/
Using the URLconf defined in trydjango.urls, Django tried these URL patterns, in this order:
products/<int:my_id> [name='product']
products/ [name='product-list']
products/<int:my_id>/delete/ [name='product-delete']
about/
[name='home']
contact/
admin/
create/
initial/
The current path, products/1/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
even though i have a product with the id=1
saved. I'm using the URL that I have configured in URL's and I can see that a product with the correct id exists when I access it from the page http://127.0.0.1:8000/products/
, where I can see a list of all products with their id's.
This is my url's:
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import (
product_list_view,
product_delete_view,
product_create_view,
render_initial_data,
dynamic_lookup_view,
)
urlpatterns = [
path("products/<int:my_id>", dynamic_lookup_view, name='product'),
path("products/", product_list_view, name="product-list"),
path("products/<int:my_id>/delete/", product_delete_view, name="product-delete"),
path("about/", about_view),
path("", home_view, name="home"),
path("contact/", contact_view),
path("admin/", admin.site.urls),
path("create/", product_create_view),
path("initial/", render_initial_data),
]
and my settings:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '$nvha^)-dy*&#_@asdgrh+!63ljr0sl@py8=%z8!si$day)*%z'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products',
'pages',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'trydjango.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'trydjango.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
Can anybody see what I'm missing?
Upvotes: 0
Views: 1199
Reputation: 21807
Your url pattern is: products/<int:my_id>
and the requested url is products/1/
. What's the difference here? Well the requested url ends with a trailing slash (/
), while the pattern does not.
Even if you made a request to products/1
you would still be redirected to the url with the trailing slash as Django by default redirects all urls without a trailing slash to the same url with a trailing slash appended. Hence simply change the pattern to have a trailing slash:
path("products/<int:my_id>/", dynamic_lookup_view, name='product'),
Upvotes: 2