I have excel files in my static folder that I read from to get some data but the files arent there when I deploy my project
these are my settings for configuring static files
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
this is how I call them in my views
from django.conf import settings
from django.templatetags.static import static
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.staticfiles.finders import find
def get_static(path):
if settings.DEBUG:
return find(path)
else:
return static(path)
def sport_news(request):
path = 'excel\sport_update.xlsx'
excel_data = excel(get_static(path))
return render(request, 'news/news.html', {"excel_data":excel_data, 'type':'Sport News'})
please check in you settings.py
but first
pip install whitenoise
in middleware
'whitenoise.middleware.WhiteNoiseMiddleware',
in bottom
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
then your main projects urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and please make sure you do have a static cdn folder in your settings.py
then run python manage.py collectstatic
if you done all the thing mentioned above and still file is not please make sure that file is in the static cdn folder and if not push the code again after completing all the step and you will be good to go
if things still did not work for you tell me
Related
I have recently put my website into production. I am using a company to host my projects on their servers and everything is working perfectly apart from when I upload images.
The uploading its self works and they are uploaded to the base/static/images/images folder correctly. But the website its self when uploading through django admin are trying to pull the images from the public_html/images/images folder. What would I need to change for either the images to be pulled from correct folder or images to be uploaded to correct folder. Below is my settings for my static files.
STATIC_URL = 'static/'
MEDIA_URL = 'images/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Try placing this in your main urls.py file:
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
I have been trying to test my Django project before deploying it on a cpanel
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
====== in project 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
from django.urls import path
urlpatterns = [
path("", include("myapp.urls")),
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)
urlpatterns modification to serve static files is recommended in development.
If you want to serve your static files from the same server that’s already serving your site, the process may look something like:
Push your code up to the deployment server.
On the server, run collectstatic to copy all the static files into
STATIC_ROOT.
Configure your web server to serve the files in STATIC_ROOT under
the URL STATIC_URL. For example, here’s how to do this with Apache
and mod_wsgi.
How to use Django with Apache and mod_wsgi
when deployed server then static files not working and media files not diplaying. 404 error
here is the urls.py
from django.views.static import serve
import django
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path ('' , include('home.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings.py
STATIC_URL = 'static/'
STATIC_ROOT = '/usr/local/lsws/Example/html/demo/static'
"""STATICFILES_DIRS=(
BASE_DIR / "static",
)"""
MEDIA_URL = 'media/'
MEDIA_ROOT = '/usr/local/lsws/Example/html/demo/static/media'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CKEDITOR_UPLOAD_PATH = 'uploads'
Verify that on prod it's the same folder and the user running webserver has access to this folder. Consider using BASE_DIR instead of absolute paths. Also you might prefer to store projects files not inside user private home folder and store them somewhere /var/opt or whatever. If you had media files locally before deployment then they must be copied to prod manually. –
Thank you #Ivan-Starostin
I created static directory which included css and image files in my Django project. When I run my project the css do display("GET /static/website/barber.css HTTP/1.1" 200 69) but the images do not("GET /static/website/images/balloons.gif HTTP/1.1" 404 1840),
in your setting.py file define STATIC_URL
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'boot'),
]
STATTC_URL = '/static/'
and add this to your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#your url
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Add STATIC settings
You need to create these definitions in settings.py to tell Django where is static files:
STATIC_DIR = os.path.join (BASE_DIR, "static")
STATIC_ROOT = os.path.join (BASE_DIR,'static files')
STATIC_URL ='/static/'# path to read css with local (probably)
STATICFILES_DIRS = [
os.path.join (BASE_DIR, "static"),
]
Be sure to import settings and static in urls.py.
You also need to add them to your main urls.py to access the URLs of images:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
#Your urls
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Deployed site works great! however when I run the development server it does not load the statics. when accessing
localhost:9090/static
it returns 404 page not found and says:
Directory indexes are not allowed here.
Please help!
STATIC_ROOT = '/var/mysite/static/'
STATIC_URL = '/static/
DEBUG = True
tried:
-collectstatic method
-adding static_dir & urlpatterns += staticfiles_urlpatterns()
I'm new to Django but, what I do have in my settings(and static files work) is the following:
(I do have a folder called static in the top directory of my project)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/path/to/static/',
)
In urls.py
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from yourprojectname import settings
from django.views.generic.base import TemplateView
urlpatterns = patterns('',
# url(r'^$', 'yourprojectname.views.home', name='home'),
# url(r'^blog/$', blog),
)
urlpatterns += staticfiles_urlpatterns()
In settings.py
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR,'static'), # if you name your static files folder is named as 'static'
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR,'template'),
)