I am getting Page not found (404) error after I set up the media folder as follows in settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
then in urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT).
After this when I try to run the server, I am getting the following message:
Using the URLconf defined in personal_portfolio.URLs, Django tried these URL patterns, in this order:
admin/
^media/(?P<path>.*)$
The empty path didn’t match any of these.
Where is the mistake?
Related
My Django files are loading after upload and are shown in the media folder, however I cannot access them at localhost:<PORT>/media/<FILENAME._EXT>. I've looked at several other answers on stackoverflow and they haven't worked. For example adding the urlpatterns += static(...), having DEBUG=True in settings.py.
When accessing: http://localhost:8000/media/controller.gif:
Error:
lightchan-backend-1 | Not Found: /media/controller.gif
lightchan-backend-1 | 2022-03-06 16:37:34,875 WARNING Not Found: /media/controller.gif
In settings.py:
DEBUG = True
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
In my urls.py:
from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('comment/<int:comment_id>/', views.comment, name='comment'),
path('comments/', views.comments, name='comments'),
path('reply/<int:incoming_id>/', views.reply, name='reply')
]
# if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
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.
Im trying to host my site on heroku on my localhost my project is fine with no issues , on deploying it all I get are blank pages (I inspected it and elements are shown but nothing is visible on webpage)apart from admin pages I installed all the necessary packages as I was deploying that is to say gunicorn and whitenoise as well as setting STATIC_ROOT and STATICFILES_STORAGE . I at first set DISABLE_COLLECTSTATIC = 1 and later set it to 0 when deploying, I have failed to see any error that might be causing the issue
some of my files for context
settings
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_dev')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import handler404
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('user.urls')),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
path('', include('pages.urls')),
path('store/', include('store.urls')),
#path("djangorave/", include("djangorave.urls", namespace="djangorave")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#error handlers
handler404 = 'store.views.error_404'
#handler500 = 'store.views.my_custom_error_view'
Whenever I try to load a static file, it gives error 404 not found. However when checking what directory it looks in it shows /account/login/static/accounts/style.css, when account/login isn't even a directory. The error message is Not Found: /account/login/static/accounts/style.css
I've searched everywhere and can't find how to change the directory.
Here is the settings.py file:
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/accounts'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Here is my accounts/urls.py:
from django.contrib.auth.views import LoginView
from django.urls import path
from accounts import views
app_name = "users"
urlpatterns = [
path('', views.home, name='home'),
path('login/', LoginView.as_view(template_name='accounts/login.html'), name="login")
]
Here is my tutorial/urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accounts.urls'))
]
And here is my view.py:
from django.shortcuts import render, HttpResponse
def home(request):
return render(r[enter image description here][1]equest, "accounts/login.html")[enter image description here][1]
Here is a image of my directory/files: https://i.stack.imgur.com/vdk0X.png
Run command:
python manage.py runserver
and then clear that account prefix from your template addresses.
change this accounts/login.html to this login.html
there is more changes you should do in your template file login.html to load static files. I recommend you first read the docs.
I'm using django admin site (v1.9.6) and recently installed file-browser for managing images and documents. I followed installation steps and file-browser is shown in admin interface but I cant upload images and documents. When trying to access them 404 errors shows up (also thumbnails are not shown in file-browser page):
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/media/uploads/aaaaaaaaaaalula.png
But the file is there, if I make
ls media/uploads/aaaaaaaaaaalula.png
it shows the file, so it's uploaded.
my settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
and url.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from filebrowser.sites import site
urlpatterns = [
url(r'^admin/filebrowser', include(site.urls)),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include(admin.site.urls)),
]
Thanks in advance for help.
C.
Ok, here is the solution:
https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development
The issue was that I was testing django-filebrowser with development server
You are not putting url for displaying image.
In setting.py-
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = '/media/'
In URLs.py--
Url(r'^media/(?^<path>.*)$','django.views.static.serve',{'document_root':
settings.MEDIA_ROOT,}),
I think it can help you
You can do:
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
#your urls
]
if settings.DEBUG: # will be 'True' in development
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)