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'),
)
Related
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)
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
I tried following this or this or this but nothing works for me - I have a directory called media which has uploaded files in it - but I cannot access it with http://127.0.0.1:8000/media/ - Page not found (404).
My settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'PROJECT/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
MEDIA_URL = "/media/"
and my urls.py:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView,
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('home/', RedirectView.as_view(url='/')),
path('admin/', admin.site.urls),
path('', include('appname.urls')),
path('', include('django.contrib.auth.urls')),
path(r'^login', auth_views.LoginView.as_view(template_name='registration/login.html')),
]
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Bonus question because I could not try it until now: How would I access an image with filename.jpg in /media/ in a template?
I fixed the issue with trial and error:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'projectname/static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, "projectname", "media")
MEDIA_URL = "/media/"
try it worked
your_app urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
....
]
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, "staticfiles")
)
MEDIA_URL = "/media/"
MEDIA_ROOT = (
os.path.join(BASE_DIR, "media")
)
I have a problem with my static and media settings, so my uploaded images doesn't show up on my site page.
In my "settings.py" I have:
MEDIA_ROOT = (
os.path.join(BASE_DIR, '..', 'static', 'media')
)
MEDIA_URL = '/media/'
STATIC_ROOT = (
os.path.join(BASE_DIR, '..', 'static'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
In my "models.py" I have:
expert_photo = models.ImageField(upload_to='profiles')
Some "urls.py":
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^experts/all/$', 'expert.views.experts', name='experts'),
url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),
)
And after all of that, when I go to my page, I see, that the picture have link, like this:
http://127.0.0.1:8000/static/profiles/i_vagin.jpg
But it must be:
http://127.0.0.1:8000/static/media/profiles/i_vagin.jpg
So how can I fix that?
Media files are not, by default, served during development. To enable media file serving, you need to add the following code in your urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Source: Django docs
Update
You'll also need to access images in your templates like this: {{ expert_photo.url }}
Just upgraded to Django 1.4, and having serious trouble with the new 'improved' serving of static and media files on development server. I love Django, but why on earth they have made serving these files doubly more complicated with STATIC_URL,STATIC_ROOT, STATICFILES_DIR now is utterly beyond me.
I'm simply trying to serve all files, static and uploaded, on the development server. I have the STATIC_URL files working, after much experimentation, but I simply cannot get the MEDIA_URL files to be served as well.
Settings:
DIRNAME = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(DIRNAME, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
I've got the media and static context processors added:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
'django.core.context_processors.media',
'django.core.context_processors.static',
"django.core.context_processors.request",
'satchmo_store.shop.context_processors.settings',
'django.contrib.messages.context_processors.messages',
)
and I've added in the url confs:
# serve static and uploaded files in DEV
urlpatterns += staticfiles_urlpatterns()
urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
with the two conf settings added as indicated in the docs, first one for static, second for media.
my structure, website being an app and static dir placed inside it as instructed on djangoproject:
<myproject>
--media
--settings
--templates
--website
|->static
In templates I can serve static content no problem with
{{STATIC_URL}}css/style.css
But any uploaded image, this one using photologue, is not served, but the urls are correct:
/media/photologue/photos/cache/spawning-2_admin_thumbnail.jpg
That directory structure does exit under media/
Super, super confused. It all seems so ridiculously complicated now, whereas I never had any issues before.
I´m very new to Django and I´ve never had any problem with static content.
This is my configuration. I hope it can help
My folder structure
django-project
--mainapp
----settings.py
----wsgi.py
----[...]
--otherapp
--fixtures
--static
--templates
--manage.py
--requirements.txt
settings.py
import os, socket
DEBUG = True
TEMPLATE_DEBUG = DEBUG
MAIN_APP = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.abspath(os.path.join(MAIN_APP, ".."))
MY_LOCALHOST = "VirusVault.local" # this is the real name of my local machine :)
try: HOST_NAME = socket.gethostname()
except: HOST_NAME = "localhost"
[...]
if HOST_NAME == MY_LOCALHOST:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = "/static/"
MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media/')
MEDIA_URL = "/media/"
else:
STATIC_ROOT = "/server/path/to/static/files"
STATIC_URL = "http://server.com/static/"
MEDIA_ROOT = "/server/path/to/static/files/media/"
MEDIA_URL = 'http://server.com/static/media/'
You need to add 'django.contrib.staticfiles' to INSTALLED_APPS
urls.py
if settings.HOST_NAME == settings.MY_LOCALHOST:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
)
If you are using Django 1.4 folder structure, you've moved settings.py to your new website app folder which means your MEDIA_ROOT is now incorrect. Not sure if a relative location works in this case, but it should be something like this
MEDIA_ROOT = os.path.join(DIRNAME, '../media/')
It might be simpler to use an absolute path.
I intended to write a comment first but somehow add comment button doesn't work.
Did you check the permissions of media directory?
Since it became an answer, I am going to dump one of my perfectly working Django 1.4 sites configuration.
structure:
-myproject
-- media
-- static
-- templates
-- myproject
--- settings.py
--- urls.py
settings.py:
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) # Not the best way but works
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'
urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
...
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
urlpatterns += staticfiles_urlpatterns()