I'm trying to create a media page on my site through Django. I'm following a course and I'm pretty stumped. I'm following exactly what the guy is doing but I keep getting the following error:
Error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
^media/(?P<path>.*)$
My settings.py:
MEDIA_ROOT = BASE_DIR/'media'
MEDIA_URL = '/media/'
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
The problem arises as soon as I enter this line:
+ static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
I haven't written anything in the views.py folders yet because the course hasn't required me to on this project so far.
I'm just a newbie so I don't really have any clue what's happening when the code looks correct but I'm still running into the issue.
Any help is appreciated.
As the error message says, your site has these 2 address available for access.
admin/
^media/(?P<path>.*)$
However, you are requesting for http://127.0.0.1:8000/ instead.
Maybe try http://127.0.0.1:8000/media/image.jpg for exmaple.
Related
When I go to this URL http://127.0.0.1:8000/job/all I am always receiving a 404 error with this message:
404 Django Error screenshot
However, when I just go to http://127.0.0.1:8000/ or http://127.0.0.1:8000 it always finds the / (index) route correctly regardless if there is a trailing slash, or not. This issue seems to only happen with the /job/WHATEVER_ELSE_GOES_HERE URLs.
I have the following URLs setup:
My jobs app urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path('', index, name="index"),
path('job/<int:job_id>', job_details, name="job_detail"),
path('job/all/', all_jobs, name="all_jobs"),
path('job/add/', add_job, name="add_job"),
path('job/my_posted_jobs/', my_posted_jobs, name="my_posted_jobs"),
path('job/delete/<int:job_id>', delete_job, name="delete_job"),
path('job/search', search_job, name="search_job"),
path('job/apply_for_job/<int:job_id>', apply_for_job, name="apply_for_job")
]
My project's urls.py:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('jobs.urls')),
path('user/', include('users.urls')),
path('payments/', include('payments.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In the error message (screenshot) it also says this:
The current path, job/all, matched the last one.
How come it matches a URL but returns 404?
How do I fix it?
Thank you
You have to include the "/" since you have included it in your syntax in your urls.py therefore call the url from your web browser using the link below when you have started the Django web server.
http://127.0.0.1:8000/job/all/
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.
My project is named 'tweetme', and when I runserver I get this error.
Using the URLconf defined in tweetme.urls, Django tried these URL patterns, in this order:
^admin/
^static/(?P<path>.*)$
The empty path 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.
urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += (static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT))
I expect to shows: the install worked successfully! Congratulation
but its shows me: Page not found at/
You have defined only two url patterns in your config. So only those will successfully work as expected. For url / to work correctly, you will have to add additional url pattern as mentioned on Django documentation using " " or "/" in relative url
https://docs.djangoproject.com/en/2.2/topics/http/urls/#url-namespaces-and-included-urlconfs
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'm trying to learn to work with Django and i'm making a image app.
Now I managed to upload images to my database, but now I want to retrieve them.
The images are stored in media/images/.
Now I made an admin page that looks like this:
AS you can see the thumbnails don't load.
When You click on one of those thumbnails, I get the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/images/june.png
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, media/images/june.png, didn't match any of these.
Here is my urls.py of my main thinghy (not the app, wich is called photo):
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Now I'm not sure what I exactly need to do to fix this.
I hope you can help me.
Please be clear ( small steps ) cause I'm not that experienced with django.
Thanks in advance
Oh BTW i'm on Win7
EDIT:
this is what I added to my settings/py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/http://127.0.0.1:8000/media/'
So right now, your urls.py doesn't have a pattern for /media/images. That pattern probably exists somewhere in your photo app. You need to link the urls for your photo app into your primary urls.py, something like:
url(r'^media/', include('photo.urls')),
This would tell the app that if it sees a url with media/ it should look for the next thing ('images/') in the photo app.