I deployed my Django app on server with Dokku. Staticfiles seems to be working as CSS is perfectly fine in admin dashboard, I can upload file but I cannot reach the file.
I discovered that I should be using Dokku persistence storage anyway. So I set it up (I suppose). Then I setup my Nginx. Before Nginx, Django was showing "page not found" for the file path. Now, I get "The page you were looking for doesn't exist.".
How can I correctly connect my app to persistence storage?
Edit:
I manually added a file to /var/lib/dokku/data/storage/dokkuappname and was able to download. So Nginx is working but Django is not using storage folder as media folder.
Static and media settings on Django
# STATIC
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR / "staticfiles")
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [str(APPS_DIR / "static")]
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
# MEDIA
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR / "media")
# https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = "/media/"
Nginx media.conf settings
location /media/ {
alias /var/lib/dokku/data/storage/dokkuappname/;
}
dokku storage:list dokkuappname result
dokkuappname volume bind-mounts:
/var/lib/dokku/data/storage/dokkuappname:/media
Solution
I wanted to see the path to Django's media folder. I put print(MEDIA_ROOT) in base.py to print the path during deployment. It was /app/my_app_dir/media. I realized that /app existed in Dokku documentation, but I always thought that it was a place holder like path/to/app. No, it was the path.
Dokku mount example
dokku storage:mount app-name /var/lib/dokku/data/storage/node-js-app:/app/storage
I fixed my mount command:
dokku storage:mount my_app/var/lib/dokku/data/storage/my_app:/app/my_app_dir/media
and it finally worked!
Related
I have setup my Django project on CPanel.
On my python setup page on Cpanel,
I have mentioned my Application Root as "example" (where i have uploaded all files)
and my Application Url as "example.com"
settings.py contains
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py under my app contains
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my models.py contains a field
documentFile = models.FileField(upload_to='documents/files/completed/%Y/%m/%d')
Now I can see two folders on CPANEL
example (where i have uploaded all my files)
example.com (I have not made any changes to it)
I was able to visit example.com, and everything works absolutely fine.
I was able to upload to file to 'documents/files/completed/%Y/%m/%d'
When i check i can physically see that the file is created under the folder example
But i am not able to fetch it back because, when i am trying to fetch the uploaded file, its actually tyring to fetch from example.com
I am new to Django, CPanel..
Changes/Suggestion please
I am trying to serve dynamically created files on my Django server.
The code works fine and serves the image file from media folder on my local server.
It also works and serves the image file in Production server but only when I set debug to True in Prod server.
But when I set debug to False in Prod Server, I get a file not found error (in console):
GET http://thesitename/media/imgs/img.png 404 (Not Found)
The server serves the files from the /static folder, but does not serve the files from /media folder.
my settings.py looks like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "frontend/media/")
urls.py looks like this:
app_name = 'frontend'
urlpatterns = [ ###urls###
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Views.py looks like this:
def template_page(request, loation, par, date):
### generating image using pil here ###
pil_image.save("frontend/media/imgs/img.png”)
path_to_image_passing_to_template = “/media/imgs/img.png”
params = {
'path_to_image_passing_to_template' : path_to_image_passing_to_template
}
return render(request, 'frontend/template_page.html', params)
html looks like this:
<img src="{{path_to_image_passing_to_template}}" />
and file structure is like this:
/backend
urls.py
views.py
/frontend
/static
/media
/imgs
/templates
/frontend
urls.py
views.py
/thesite
settings.py
urls.py
wsgi.py
/static
I don't understand why it should work with debugging turned on in prod server and not otherwise!!!
I have looked at several questions but did not find any query close to this kind of issue. I took some help while writing working out the dynamic media files code from this answer earlier but am stuck right now.
Confirm that you have 'os.environ' module loaded...try executing the
code below on the production server.
import os
print(os.environ.get('MEDIA_ROOT'))
Make sure it shows the expected path.
then ensure that your 'urls.py' and 'Views.py' have 'import os' line appearing at the top of the file. Good Luck!
I'm struggling getting staticfiles (CSS) to work on my Django site deployed to Openshift. I can workaround this by putting style info in the HTML templates, but the Django admin site stylesheets still will not load. Everything works locally, including with debug off.
Openshift stores staticfilesunder repo/ -> wsgi/ -> static/, where the main project directory is also under wsgi/.
Here are the relevant parts of settings.py:
ON_OPENSHIFT = True if 'OPENSHIFT_REPO_DIR' in os.environ else False
PROJECT_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)
if ON_OPENSHIFT:
STATIC_ROOT = os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'static')
else:
STATIC_ROOT = ''
STATIC_URL = '/static/'
if ON_OPENSHIFT:
STATICFILES_DIRS = (os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'static'))
else:
STATICFILES_DIRS = [os.path.join(PROJECT_DIR, 'static')]
I have a script that runs the following when deploying:
cd $OPENSHIFT_REPO_DIR/wsgi/squadron
python manage.py collectstatic --noinput
I'm using Python 3.3 and Django 1.6, and referencing the following guides: 1 and 2 from the docs.
What am I doing wrong? I don't get an error message, just no stylesheets, either my own, or on Django admin. When I use SCP, I can verify my stylesheet is in the correct Openshift directory.
This is the best source to read when you are trying to get OpenShift working with Django.
http://appsembler.com/blog/django-deployment-using-openshift/
Some of this will be changing (for the better) with upcoming releases but this should help for now.
I am using django 1.3 and trying to deploy a django project (client sent) on my dev machine (ubuntu 12.04). The problem is regarding the static files. My directory structure is as follows:
project_name
media
static
css
img
js
settings.py
Here is my settings.py:
ROOT = '/home/user/project_name'
MEDIA_ROOT = '%s/media/' % ROOT
MEDIA_URL = '/media/'
STATIC_ROOT = '%s/static/' % ROOT
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
My site is perfectly deployed but the css, js and imgs are missing. Same is the case for the admin interface. When I use the link http://mysite.com/static/js/some.js it gives a 404.
Help would be appreciated and up-voting an answer is custom.
I think you need to run ./manage.py collectstatic :)
You don't mention configuring your web server to actually serve the static files. You need to point it at the directory that collectstatic put them into.
did you run python manage.py collectstatic ? see here
be careful that in production you should place the static file in a static server. There should be something in the guidelines.
And a little offtopic..
It will be better, to use:
MEDIA_ROOT = os.path.join(os.path.dirname(file),'media').replace('\','/')
STATIC_ROOT = os.path.join(os.path.dirname(file),'static').replace('\','/')
and in main urls.py at development, django webserver only:
urlpatterns = patterns('',
(r'^media/(?P.*)','django.views.static.serve',{'document_root': os.path.join(os.path.dirname(file),'media').replace('\','/') }),
(r'^static/(?P.*)','django.views.static.serve',{'document_root': os.path.join(os.path.dirname(file),'static').replace('\','/')}),
In this way, u dont need collectstatic, ull need it at production server, where u will use nginx or something other to server your static
I'm trying to use django-cumulus for serving files off Rackspace CloudFiles. I'm currently only trying it on my local dev server, using Django 1.4.2.
I can use cumulus's syncstatic management command to upload all my static assets successfully, but I can't seem to display them on my site with the same settings.
If my relevant settings are:
STATIC_URL = '/static/'
CUMULUS = {
'USERNAME': 'myusername',
'API_KEY': 'myapikey',
'CONTAINER': 'mycontainername',
'STATIC_CONTAINER': 'mycontainername',
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'
then when I run syncstatic all my apps' static files are uploaded into /mycontainername/static/, as I'd expect. But when I load a page in admin it ignores STATIC_URL and tries to serve assets from URLs like http://uniquekey....r82.cf2.rackcdn.com/path/to/file.css rather than http://uniquekey....r82.cf2.rackcdn.com/static/path/to/file.css.
Also, I can't see how to have my public (non-admin) pages use the static files on CloudFiles, rather than serving them from a local /static/ directory.
Have I missed some crucial setting, or am I doing something else wrong?
I had the same problem. What i did was to
git clone https://github.com/richleland/django-cumulus.git
edit context_processors.py
from django.conf import settings
from cumulus.storage import CloudFilesStorage
def cdn_url(request):
"""
A context processor to expose the full cdn url in templates.
"""
cloudfiles_storage = CloudFilesStorage()
static_url = '/'
container_url = cloudfiles_storage._get_container_url()
cdn_url = container_url + static_url
print {'CDN_URL': cdn_url}
return {'CDN_URL': cdn_url}
Once you are done, install it with sudo python setup.py install
Do note that context_processors.py from django cumulus is actually quite slow