I have a django project based on django 1.11.
I uploaded media files and try to get them but instead I get nothong.
I read documentation and did exactly as it said.
Also I checked 'media' foleder permissions.
settings.py:
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('myapp.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Change your MEDIA_ROOT to MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') in settings.py and make sure your media file in project root directory.
Related
My site can't locating where it's static and media file after upload on shared hosting. Here is my
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/project32/rootfolder/media/' #this is the path of my hosing media root file
STATIC_ROOT = '/home/project32/rootfolder/static/' #this is the path of my hosing static root file
root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
where I am doing mistake? why my website can't locating the static folder on my shared hosing ?
I also run python manage.py collectstatic
my problem solved after adding adding STATIC_URL path in my root urls.py
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
You should delete the 'STATIC_ROOT' line. You should use it this way.
I have been trying to display my images in my html page and I did do it correct but it wont work I don't know why this is the last update of django I have watched and read alot of videos and article and do the same as they told me to do but it not working the image wont work in my html template
Hope anyone can help me with this
Thank you
admin.py
from django.contrib import admin
# Register your models here.
from .models import *
admin.site.register(slideshowgo)
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
MEDIA_ROOT = BASE_DIR / 'static/images'
models.py
from django.db import models
class slideshowgo(models.Model):
image_one = models.ImageField(null=True,blank=True)
slideshow html
<div class="slide">
<img class="mySlides imgslide" src="{{slideshowgo.image_one.url}}">
</div>
urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('card_store.url')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In settings.py put the following config
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
And in your main urls.py file ( one which is located in the same app as of settings.py )
Add this
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will work for DEBUG = True
For DEBUG = False it won't as Django cannot server media files by itself, so you need to setup NGINX or APACHE.
One workaround is to put the following URL's in your main urls.py ( Not recommended for production )
re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
I am trying to configure static files for django v2.2.1 following this https://docs.djangoproject.com/en/2.2/howto/static-files/
In settings.py:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
In urls.py:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Project directory:
But I'm getting this error when running localhost:8000
I've been scratching my head for too long.
What am I missing here?
If you want the picture as your response you will have to visit http://127.0.0.1:8000/media/no_picture.png
If you want access to the admin page visit http://127.0.0.1:8000/admin/
You aren't receiving any response on http://127.0.0.1:8000/ because you haven't defined a view for that url pattern.
To write a Hello World view for example check out this tutorial from django documentation.
You can see my urls.py and my settings.py.
Server
NGINX, gunicorn
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("appCms.urls"))
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
MEDIA_URL ='/mediafiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles_cdn/')
I somehow got the application running on django (new to python and django) and although the page loads the default URL (127.0.0.1:8000) but it does load the css files. It produces following error css files are directly accessed.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/css/bootstrap.min.css
'css\bootstrap.min.css' could not be found
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.
Here is my settings.py page:
STATIC_URL = '/static/'
# Template location
TEMPLATE_DIRS = {
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),
}
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "media")
STATICFLIES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static")
)
and Here is the urls.py page:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'signups.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
can someone help with this please?
After you add any kind of static files to your project you should execute
python manage.py collectstatic