I've started with the online tutorial on Django(1.9) from thenewboston channel on YouTube which is building a simple music app.
However I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/music
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, music, didn't match any of these.
Here are my files:
website/website/settings.py
...
INSTALLED_APPS = [
'music',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
ROOT_URLCONF = 'website.urls'
...
website/website/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^music/', include('music.urls')),
url(r'^admin/', admin.site.urls),
]
website/music/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
website/music/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>This is the Music app homepage</h1>")
P.S: I've made sure that I am editing the correct urls.py file and it is placed in the correct directory also I guess, unlike the other similar questions asked about this problem
EDIT: I renamed my root directory from "website" to "website1" just to resolve the ambiguity related to reference of "website" and music and admin sections are working fine now but the django server homepage(http://127.0.0.1:8000/) is displaying the following error now:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
^music/
^admin/
The current URL, , didn't match any of these.
EDIT 2: Issue fixed, renamed the root directory from "website" to "website1". Seems Django was confused by multiple directories named "website"
Everything you did is correct. Make sure your files are saved and restart the server. If it's still not working, you can instead import the music urls like so:
from music import urls as music_urls
And then change the music url line to this:
url(r'^music/', include(music_urls)),
Only a partial answer: The error message lists all the urls that are known. In your case, only
^admin/
is known (according to the error message you posted), it does not know about ^music/. I don't see why it does not know about ^music/, though.
Edit: Although the include(string) should work, you could try to replace it with include(music.urls) and add an import statement on music.urls.
Related
I am a total beginner in "django" so I'm following some tutorials currently I' am watching https://youtu.be/JT80XhYJdBw Clever Programmer's tutorial which he follows django tutorial
Everything was cool until making a polls url
Code of views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
HttpResponse("Hello World.You're at the polls index")
Code of polls\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Code of Mypr\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/',admin.site.urls),
path('polls/',include('polls.urls')),
]
I don't get it I did the same thing but I'm getting error not only polls.In one turtorial he decided to make blog,and again the same error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in
this order:
admin/
The current path, polls/, didn't match any of these.
Please my seniors help me.
Note:I'm using the latest versions of django,windows and as editor I'm using Pycharm.
Already tried(and did not work):
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in Mypr.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, 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.
try to access polls/ URL in your browser then you will access the page
Because you have accessed the URL of your project, you have to go to this URL to access your app
try by changing your code like this
from django.urls import path
from polls.views import index
urlpatterns = [
path('', index, name='index'),
]
I was facing the same error. Feel kinda dumb after figuring out how to solve this.
Your urls.py content is all right
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
But it must be included in the mysite\mysite\urls.py and not in mysite\urls.py.
That means the inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.
Hello I'm making a portfolio website with Django, it was going fine but in the last steps following a guide I got this error
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:
admin/
projects/
The empty path didn't match any of these.
This is my project folder
Here is personal_portfolio - urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("projects/", include("projects.urls")),
]
And projects - urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.project_index, name="project_index"),
path("int:pk>/", views.project_detail, name="project_detail"),
]
Also the section in settings.py
> INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projects',
]
Here is the link of the repo of the guide I'm following https://github.com/realpython/materials/tree/master/rp-portfolio
I downloaded the repo and tried to run the server and got the same error, how?
Also I searched here people with the same error even someone who was following the same guide but their solution didn't help mine and it was the same project
A request to the http://localhost:8000/ endpoint will indeed not trigger a view, since the project_index view is located under the http://localhost:8000/projects/ endpoint.
If you want to change this, you should alter the path to the projects.url with:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('projects.urls')),
]
There is also a typo in the projects/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_index, name='project_index'),
path('<int:pk>/', views.project_detail, name='project_detail'),
]
I was creating a basic website following this tutorial:
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
When I tried to redirect the home page to my django app called unihub this error prompted:
Using the URLconf defined in TFGsWeb.urls, Django tried these URL patterns, in this order:
admin/
unihub/
^static\/(?P<path>.*)$
The current path, catalog/, didn't match any of these.
My files look like this:
/TFGsWeb/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'unihub.apps.UnihubConfig',
]
ROOT_URLCONF = 'TFGsWeb.urls'
STATIC_URL = '/static/'
/TFGsWeb/urls.py
from django.contrib import admin
from django.urls import path
# Use include() to add paths from the unihub application
from django.urls import include
# Add URL maps to redirect the base URL to our application
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('unihub/', include('unihub.urls')),
path('', RedirectView.as_view(url='/unihub/', permanent=True)),
]
# Use static() to add url mapping to serve static files during development (only)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
/TFGsWeb/unihub/urls.py
from django.urls import path
from . import views
urlpatterns = [
]
I don't understand why this catalog/ appears when I see no reference to any app or path named catalog/ in the files I did modify.
I'd also like to upload this project to my github so should I just do this for the keys or sensible settings info?
with open('./secret_key.txt') as f:
SECRET_KEY = f.read().strip()
Your unihub application on the mentioned url (/) might be calling (some ajax) or redirecting some catalog app url (specifically catalog/).
Include in your urls the urls from catalog app and/or add such app to your installed apps.
On the other hand, you don't need a RedirectView to point / to your view just write:
urlpatterns = [
path('admin/', admin.site.urls),
path('unihub/', include('unihub.urls')),
path('', include('unihub.urls')),
]
Solution:
When I tried to reproduce the "error" in other browsers it didn't happen so my guess is that...
Was not the app itself but the web browser that stored http://127.0.0.1:8000/catalog/ as the link to go to if the web does not have a valid redirect.
Thanks everyone for the support, I changed the url patterns to the suggested ones.
Here come my urls.py page code:
from django.contrib import admin
from django.urls import path
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
]
When I run Django I get 404 error:
Page not found (404) Request
Method: GET Request URL: http://localhost:51169/admin Using the
URLconf defined in DjangoWebProject3.urls, Django tried these URL
patterns, in this order:
admin/ The current path, admin, didn't match any of these.
I know this is a famous problem but couldn't find any answer for it.
What is wrong?
The admin url is http://localhost:51169/admin/. You are missing the trailing slash.
Normally, Django should redirect from /admin to /admin/. If it hasn't it suggests that you've changed something in the settings, or you've upgraded Django from an older version without switching from MIDDLEWARE_CLASSES to MIDDLEWARE in your settings.py.
As Alasdair commented I updated my Django but didn't update my settings before:
Know:
In the setting, I changed middleware_classes to middleware and commented one of the middlewares: #'django.contrib.auth.middleware.SessionAuthenticationMiddleware'
now login page works!!
thank you,
I am currently trying to complete the Django 2.0 tutorial Poll application. I am using Django version 2.0.7 and Python 3.7.0. I have set up a virtual environment for this project. I am very new to Django so apologies if my terminology is incorrect.
Basically, I cant get part 1 to work.
I have tried numerous times to run it and I cant seem to figure out why it wont work.
Before I create the Polls app the site runs correctly and I get the rocket ship, however after I create the app, update polls/views.py, create the polls/urls.py file and update the mysite/urls.py, I get the following error:
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:
polls/
admin/
The empty path didn't match any of these.
I thought my code was incorrect, so I copied and pasted the code from the website directly and tried to run it. It still wont work.
Below is the code I am trying and the directory layout. Any help would be greatly appreciated. I'm sure I am just missing something simple.
Thank you.
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'), ]
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
print.txt
settings.py
urls.py
wsgi.py
init.py
pycache
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\polls
admin.py
apps.py
migrations
models.py
tests.py
urls.py
views.py
init.py
pycache
have you added your app name inside settings.py make sure you have added the name in settings.py which resides under project folder
in your case it will be here:
Directory of C:\Users\ad\Documents\projects\mysite\msenv\mysite\mysite
like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',] # <--- polls in your case
make sure you are using your routes you have defined to access the correct page
for example in your case:
your website's routes are:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
and your application 'polls' routes are:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
according to your views.py file
which is this:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
you must visit http://localhost:{port number}/polls/ or http://127.0.0.1:{port number}/polls/
default port number is 8000
after running server using python manage.py runserver
to get your page as an output on browser