Have a problem with connecting css to my template.
My project root is
"D:/birzha/", static path is "D:/birzha/static/", css in
"D:/birzha/static/css/template.css".
What STATIC_ROOT or STATICFILES_DIRS should I use for correctly viewing css file? I tried so much turns, but nothing happens, css still off.
First of all you need to tell where all your static files will be "collected", place the following lines in settings.py:
BASE_DIR = (os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Then you need to make a static folder within your app and the hierarchy
should be like this appname/static/appname/css.
Finally run the command python manage.py collectstatic and type yes on prompt. This will copy all of your static files within a folder that you specified in STATIC_ROOT
Now you can access your static files by giving the path like,/static/appname/css/mystyle.css
You can try this
Site root will contain you project root path. You can change "/templates" with static
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
# calculated paths for django and the site
# used as starting points for various other paths
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates/'),
)
MEDIA_ROOT = os.path.join(SITE_ROOT, 'templates/media/')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SITE_ROOT, 'templates/'),
)
Related
I am trying to deploy the app on GCP but when I run the following command:
python manage.py collectstatic
It returns:
Found another file with the destination path
'admin\css\autocomplete.css'. It will be ignored since only the first
encountered file is collected. If this is not what you want, make sure
every static file has a unique path.
Found another file with the destination path 'admin\css\base.css'. It
will be ignored since only the first encountered file is collected. If
this is not what you want, make sure every static file has a unique
path.
And many other like this.
Here's my Settings.py
STATIC_URL = 'https://storage.googleapis.com/yantra-packs/static/'
# STATIC_URL = '/static/'
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), ]
Where is it that I am providing two paths for the static files?
The findstatic command will help to debug.
The default value of STATICFILES_FINDERS is:
[
'django.contrib.staticfiles.finders.FileSystemFinder', # finds files stored in the `STATICFILES_DIRS` setting.
'django.contrib.staticfiles.finders.AppDirectoriesFinder', # finds files stored in the 'static' subdirectory of each app.
]
Hence, even though you only have:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), ]
AppDirectoriesFinder will find files stored in the static subdirectory of each app.
Also, you might want to look at the Django's official documentation describing the collectstatic command.
I've been trying to deploy my site this weekend and have thus been meddling with the settings. One of the unpleasant surprises while doing this has been that my static files have seemingly stopped working on my site. My CSS files and javascript files don't work anymore, as if they aren't found by the site. The only thing I can remember doing with regards to static files was inserting this into settings.py:
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'
I removed these settings and replaced them with the original
STATIC_URL = '/static/'
but alas, the problem remains. Why isn't Django finding my static files anymore?
PS: As I'm a noob, I don't know exactly what is relevant from my project for you guys to see, but do let me know and I shall provide additional info.
Okay, update your settings.py as below:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_my_proj"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
Now, that you have done that make dir called static_my_proj, at the same level where your manage.py file is.
Okay, now create new dir called static_cdn just one level up, i.e. one level up to manage.py file.
Now, create dir called static_root inside static_cdn and that is all you have to do. Make sure you run python manage.py collectstatic
NOTE:
static_my_proj is for development or DEBUG = True and static_cdn is for production or DEBUG = True. handy, right?
Also, you can add media file the same way, just add,
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
and make new dir called media_root under static_cdn.
EDIT:
Copy all your admin files to static dir manually.
If you have virtualenv than go to <virtualenv-dir>/lib/python3.6/site-packages/django/contrib/admin/static and copy dir named admin.
It should work even if DEBUG=False
Hope this helps.
I have two directories called media and static.
When I have DEBUG = True everything works but if I change it to DEBUG = False the problems arises.
I've read that my server will handle static files in production. I have understood it as
In development it's fine to have all my static files in my directory static but when I go in production I need to move all my static files to another directory (it could be static_www?) and before starting the server I run python manage.py collectstatic. This command will move all the files from static_www to static and everything works.
But why do I need to have two separate directories with same content? How do the server know that my files 'has been collected' through collectstatic (it's just files in a folder so how can it know the difference)? I guess collectstatic is primarily used when you have static files in multiple directories and want to collect them all before you go into production.
The variables in my settings.py looks like:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_www"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
and it moves from static_www to static so neither in production nor development my directory static_www is not used.
Maybe all these settings are meant for websites which has another server only for handling static files?
STATIC_ROOT is where the virtual directory for STATIC_URL will point.
Example:
STATIC_ROOT = '/var/www/example.com/static/'
STATIC_URL = 'http://www.example.com/static/'
collectstatic will find all static files in STATICFILES_DIR and copy to STATIC_ROOT.
A request to http://www.example.com/static/file.jpg will be handled by Django and /var/www/example.com/static/file.jpg will be served.
You can set your static files to be even in another site, using something like:
STATIC_ROOT = '/var/www/static.example.com/static/'
STATIC_URL = 'http://static.example.com/static/'
In this way, static.example.com can be a simple server which will serve files without they be handled by Django, increasing the site performance. Or you can upload to a CDN like Amazon S3 (and CloudFront), etc.
I am trying to read create mp3 files in django. but I am confused about static and static_root that I have configured.
WHat happening is that in my code at a point when I print the below line it shows
/usr/local/src/mena_recording/play/static/audio/dorris_0_.mp3
code:
print settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3'
but when I use the same thing in the next line in this piece it gives this error:
IOError at /
[Errno 2] No such file or directory: u'/usr/local/src/mena_recording/play/static_root/play/static/audio/dorris_0_.oga'
code:
with open(settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3', 'w') as mp3_file:
mp3_file.write(decoded_mp3_str)
mp3_file.close()
my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mena_recording/static'),
os.path.join(BASE_DIR, 'play/static'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
Would someone enlighten me please how this works ?
Thank you.
From the django docs,
STATIC_ROOT is the absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_URL is the URL to use when referring to static files located in STATIC_ROOT.
So, when you request some specific static resource, it is searched in STATIC_ROOT + STATIC_URL and then served.
Now in your problem, you do
STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'
which means django would have effectively been searching in BASE_DIR/play/static_root/static/ which would be incorrect, so looking at other paths you can figure out that you need to do
STATIC_ROOT = os.path.join(BASE_DIR, 'play/')
I wanted to enable serving static files through django 1.5 builtin development server. I came across a strange problem.
If I make a request to my static file using localhost:8000/static/staticstyle.css then it responds me with 404 not found. But If I make a request to the same file without the value mentioned in the STATIC_URL and add the url pattern as "url(r'^anyval_other_than_mentioned_STATIC_URL/', django.views.static.serve,{'document_setting':settings.STATIC_ROOT}), ", then it responds me with 304 , which is conditional get.
I have collected all the static files in the static directory setup in the settings file and enabled all the context processor required for the template.
For production I have used nginx to serve the static file so there is no problem.
My settings.py looks like
STATIC_ROOT = APPLICATION_PATH+"/static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('vendors',APPLICATION_PATH+'/vendors'),
('admin/assets/',APPLICATION_PATH+'/templates/admin/assets'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
My application urls.py looks like
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any hints would be highly appreciated.
Thank you .
please read this first
https://docs.djangoproject.com/en/1.5/howto/static-files/
There are some settings related to static files serve
The list of finder backends that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Absolute path to the directory static files should be collected to
STATIC_ROOT = os.path.join(APPLICATION_PATH, '..', 'static')
URL prefix for static files.
STATIC_URL = '/static/'
You can specify Additional locations of static files
STATICFILES_DIRS = (
os.path.join(APPLICATION_PATH, 'vendors'),
os.path.join(APPLICATION_PATH, '/templates/admin/assets'),
)
There is special application for static serve in django contrib
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
After You have configured your project run collectstatic command
python manage.py collectstatic
This will copy all files from your static folders into the STATIC_ROOT directory.