How can i fix Page not found 404 in django? - django

Hi ive been coding my first website and then try running the product page I get this error
*Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products
Using the URLconf defined in myshop.urls, Django tried these URL patterns, in this order:
admin/
The current path, products, 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.*
how can I solve this? Here is my code...
my views page code
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello world')
def new(request):
return HttpResponse('New Products')
productsurls code
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('new', views.new)
]
myshopurls
from django.contrib import admin
from django.urls import path, include
urlpatterns = {
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
}

Most probably, you are facing this problem because you have not include your app "products" in the "settings.py" file.
-> After checking your code I can conclude that"myshop" is your project and "products" is your app.
So, you will have to go in settings.py file under which you will find a list "INSTALLED_APPS".
Inside INSTALLED_APPS -> You will have to include your app.
Go to "apps.py" file of the "products" app and copy the name of the "config" class.
After that in your settings.py file, you will have to write 'products.apps.(paste the name of config class)'.
Most probably name of your config class will be "ProductsConfig" -> So, you will have to write 'products.apps.ProductsConfig',
Hope it will work !

You have to include the app under INSTALLED_APPS in settings.py file.
So your settings.py file should read like so
INSTALLED_APPS = [
# Built In Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# User Defined Apps
'product.apps.ProductConfig',
]
Tip - If you are confused as to how to include the name under installed Apps. It should be like so
<myapp>.apps.<name_of_class_in_myapp/apps.py_file>
Basically navigate to your app folder -> apps.py file and check the name of the class. It will be like so
from django.apps import AppConfig
class ProductConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'product'
You want the name of this class. ProductConfig.
Note - Local apps should always be added at the bottom because Django executes the
INSTALLED_APPS setting from top to bottom. We want the core Django apps to be available before our app.
The reason for listing the app like
'product.apps.ProductConfig',
is because it is best practice and helps if in future if you decide to use Django Signals.

Related

Local server won't go through my URL pattern on my local server

I'm new to Django and I'm facing a problem that I can't solve despit my research... I'v already made small django projects without this error but since I reformat my computer (I can't see the link but that's context!) i'm having this problem.
So when I'm running a django project this is what my browser shows me despite others urls in my project
Django doesn't go trhough my urls
I'v tried with an almost empty project but a least one more url and still have the same page.
urls.py in the main file (src/mynotes/urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('index/', include('dbnotes.urls')),
path('admin/', admin.site.urls),
]
settings.py in the main project (src/mynotes/settings.py):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dbnotes'
]
urls.py in the app (src/dbnotes/urls.py):
from django.urls import path
from . import views
urlpatterns = [
path("", views.homepage, name="homepage"),
]
views.py in the app (src/dbnotes/views.py):
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello, world. You're at the polls index.")
Even stranger, when i change "path('admin/', admin.site.urls)" I still get the admin page with the same route. Even if i change 'admin/' or admin.site.urls.
It looks like my local server display an over empty django project but in the error page it's written "mynotes.urls" as the url location which is the good folder.
I'm working with a virtual environment.
I'm lost as it's the first time i got this issue despite having created ohter (small) django projects!
If anyone has an idea that would be great! Thank you!
The directory:
Directory
I tried to follow the django documentation to write the smaller project and then having less room to error!
I tried with a new project and a new virtual envirnoment.
I found a topic related to my problem but without an anwer : Django doesn't see urls.py

Why is Django is not loading my template view but it once was

My view was running fine until Ill tried to override an admin view. Which i eventually got to work. However in the process I broke the path to my view that was working. The admin view had nothing to do with my originally working view.
Now I copied my view into every possible level of my project structure. Yet the django template loader fails to find my order_input.html
The error shows the correct path to my order_input.html. I made copies of order_input.html at every single possible level of my project... but django still cant find it.
APP - URLS.PY
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hw/$', views.helloworld, name='helloworld'),
url(r'^order_input/$', views.order_input, name='order_input'),
url(r'^time/$', views.today_is, name='time'),
url(r'^$', views.index, name='index'),
]
SETTINGS.PY
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
# 'django.contrib.staticfiles',
'import_export',
'hw_app',
]
PROJECT URLS.PY
urlpatterns = [
url('r', include('hw_app.urls')),
url('admin/', admin.site.urls),
path('clearfile', views.clearfile),
path('readfile', views.readfile),
path('writefile', views.writefile),
path('helloworld', views.helloworld),
path('order_input', views.order_input),
path('ajax_view', views.ajax_view),
]
You have the order_input url defined in both your project and your app urls.py files, only in the hw_app version it has a trailing slash. The slashless project URL may be looking in the wrong places for things as it will assume there is a related view at its level.
Try removing that path from the project urls.py file. Assuming it should be going to the same place, it's already included when you include(hw_apps.urls) (as url() is just a more up-to-date path() ). Then try calling the page with a trailing slash.
The only view file then relevant should be hw_apps/views.py. For consistency place the HTML template file is in hw_app/templates/hw_app/order_input.html so you know django should be able to find it.

Page not found after trying to add a new path to the urlpatterns of main urls.py file? (Mosh Python Tutorial)

I'm following Mosh's tutorial video on Python. He begins the django section (https://youtu.be/_uQrJ0TkZlc?t=18085) by installing django 2.1. I am able to open a development server the first time as he does:
pip install django==2.1
django-admin startproject pyshop .
python manage.py runserver #server works
Here are the steps he goes through to add a "products" app/path:
python manage.py startapp products
Opens views.py from this new products folder and modifies code to this:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello World')
Creates urls.py inside the products app/folder and adds this code:
from django.urls import path
from . import views
urlpatterns = [
path(' ', views.index) # Here should have been '' and now fixed
]
Opens the main urls.py in the pyshop folder and adds/modifies the end of the file like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
Mosh goes back to the server and adds /python to the url to get a page with "Hello World"
(https://youtu.be/_uQrJ0TkZlc?t=19220)
Upon trying to run the server again, I get Page not found error. Is there something I'm missing? I didn't figure it'd be a version issue since I made sure and installed the same 2.1 version.
As Jeffery and Daniel mentioned, I had an incorrect space and it needed to be an empty string in the products urls.py path. However, I'm still getting page not found error. Here's the exact error message if that helps:
Using the URLconf defined in pyshop.urls, Django tried these URL patterns, in this order:
admin/
products/
The empty path didn't match any of these.
Modify your url path in the products app/folder like this
urlpatterns = [
path('', views.index)]
Remove the space in between

Django is not invoking my app's ready method [duplicate]

Trying to catch the basics of Django. Namely how Applications work.
The docs: https://docs.djangoproject.com/en/stable/ref/applications/#methods
And in the code of the class AppConfig we can read:
def ready(self):
"""
Override this method in subclasses to run code when Django starts.
"""
Well, this is my example:
my_app/apps.py
class MyAppConfig(AppConfig):
name = 'my_app'
def ready(self):
print('My app')
I just want to make the ready method work. That is, when Django finds my_app, let it run the ready method.
The app is registered in INSTALLED_APPS.
I execute 'python manage.py runserver'. And nothing is printed.
If I place a breakpoint inside the ready method, the debugger don't stop there.
Could you help me: what is my mistake in understanding here. Thank you in advance.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
]
And I created a view
my_app/views.py
def index(request):
print('Print index')
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', my_app_views.index, name='home')
]
Well, the view is working. This means that the application is registered.
You need to do one of two things. Either explicitly say which AppConfig you want in INSTALLED_APPS:
INSTALLED_APPS = [
'my_app.apps.MyAppConfig'
]
Or, define a default_app_config in the __init__.py of your app:
# my_app/__init__.py
default_app_config = 'my_app.apps.MyAppConfig'
(and leave INSTALLED_APPS as-is).
As it is currently Django can't find any AppConfig for the app and just assumes there isn't one. So your views etc. will work, but the ready() method will never get called.
Here's the relevant section of the documentation.
Edit: as of Django 3.2, specifying a default_app_config is no longer necessary, and is in fact deprecated - so this answer is redundant for anyone using Django 3.2 or later.

how to overcome the following url issue?

Hello I am beginner learning Django I am triying to follow this tutorial:
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
I created an app called Polls to test my site:
Since I dont have idea where to put the file called urls.py
I put this file at the following directories:
Django/mysite/polls/urls.py
Django/mysite/polls/migrations/urls.py
This file contains:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And finally I added the following lines:
at this level:
Django/mysite/mysite/urls.py
this file contains:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
I dont know where is the issue but when I run:
Django/mysite$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 21, 2017 - 15:59:43
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I got in the page the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , 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.
So I really appreciate support to overcome this issue,
thanks for the support
the urls.py doesn't go into the migrations folder.
Did you register your Polls app in the settings.py file? you need to add the name of the Polls app to the INSTALLED_APPS list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'example: add app name here',
'polls',]
you then need to create a url.py file within your polls app.
polls/
urls.py
In that file you would add:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
the url you want to use in your browser would be something like this:
http://127.0.0.1:8000/polls