Django collecting static from virtualenv app on apache wgsi - django

I'm using autocomplete-light 3 in Django project.
I publish production with an virtualenv.
Autocomplete-light come with some static file in.
I added in settings:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/......../........../myapp/static',
'/home/myuser/.virtualenv/prd_vrt/lib/python2.7/site-packages/dal/static',
'/home/myuser/.virtualenv/prd_vrt/lib/python2.7/site-packages/dal_select2/static',
)
but it was not enough to publish the static files.
If I include the file in my static folder it works.
I don't think this is the best solution ( not easy maintenance).
May I add some permission to apache?
Or what is the best way to archive this situation?

Related

Using static folding inside app folding Django

I have a static directory inside my apps folder. I added this line of code to my projects settings.py file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/var/www/static/',
]
The file continues to result in a 404 not found. I'm getting stuck, what should I do?
It probably isn't mistake in your static files settings. But to be sure the relevant settings for static folder in app directories is STATICFILES_FINDERS https://docs.djangoproject.com/en/2.1/ref/settings/#staticfiles-finders. But the AppDirectoriesFinder should be present by default so it is probably OK.
Your problem is probably in your URL scheme:
For development purposes make sure you have django.contrib.staticfiles in your installed apps, DEBUG = True and STATIC_URL = '/static/' (or something similar). Then you should be able access the files.
For deploying it is more complicated but essentially you have setup STATIC_ROOT, before starting the server run python manage.py collectstatic command which will copy all static files to one location and then setup your server (Nginx) to serve the files from this folder under /static/ URL. Here is the official documentation https://docs.djangoproject.com/en/2.1/howto/static-files/#deployment
Note: if your app is foo and you have bar file in your static directory under foo app the URL for the file will be /static/foo/bar.

Django load static in development directly from static folder (Not from apps)

I use django for the backend, in the frontend I use vue.js, so 99% of my CSS it's handled by vue.js, however I need a simple base.css for some customization in the landingpage and few things like this.
Normally in django I would put the file inside app/static/app/base.css then do collectstatic and get it under static/app/ for production.
I would like to avoid to keep it under an app as it's just a file. I'm trying adding a folder under my main "static" folder. But it seems in development django in not fetching it at all, it fetches directly and only static files from apps.
How can I tell django to fetch it directly from the static main folder as it would do in production?
i.e. I want to add a folder called main in my root (where manage.py is) and use only that to store my static files for both production and development, without passing through the single apps.
You can tell Django to look for static files in other directories by using STATICFILES_DIRS settings.
Just add the following code in your settings.py and it should work:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

In Django, how can I use project wide static files?

At the moment I have
myproject/app1
And I am adding a second app in and I wish to share some common JS and CSS
I have moved my static files from
myproject/app1/static
to
myproject/static
What configuration changes do I have to make for the applications to recognize the new location? I don't remember ever setting the directory initially I think it just worked out of the box
From Django [1.11] Docs:
Your project will probably also have static assets that aren’t tied to
a particular app. In addition to using a static/ directory inside your
apps, you can define a list of directories (STATICFILES_DIRS) in your
settings file where Django will also look for static files.
Add to settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Assuming you are using django.contrib.staticfiles app.
In settings.py, declare the following:
STATIC_URL = '/static/'
Also, add django.contrib.staticfiles to your INSTALLED_APPS if it isn't already.

Serving static admin files for Django 1.5 in a shared hosting environment

I'm trying to get a fresh installation of Django 1.5 running on an Apache-Server. The WebServer is situated on a shared hosting platform called uberspace.de
which means I have no access to the Apache configuration itself I can however write .htaccess files if that's any help at all. Django is deployed via fast-cgi which is working as expected.
Whats not working however is the access to static files on the server like the .css files and graphics for the Django administration interface.
As mentioned in the official docs I used the following command to copy the static Files into my ~/html/static directory.
manage.py collectstatic
And these are the values from my settings.py:
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = '/static/'
All I get is the infamous django 404 page when I try to access any of these Files.
I also followed the 'How to install and deploy Django' guide on my Webhosters Website to the letter. (sorry its only available in german I believe)
I already contacted the webhosters support but they don't know whats wrong.
All the solutions I've come up with so far suggest setting some sort of Alias in the Apache configuration. Which I can not do.
I'm thankful for any ideas you might have.
Try using a full address instead.
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = 'http://www.mysite.com/static/'
Edit: Perhaps you could ask your host to setup /static/ in your Apache config:
sudo nano /etc/apache2/sites-enabled/mysite.com and add:
Alias /static/ /home/bier/html/static/
I've had a situation before where I've had to upload /static/ files manually because of a highly restrictive host (permissions). Perhaps you need to download a copy of django to your desktop and then upload the static admin file set into your /static/ directory manually?
Lastly, have you added the static files to your urls?
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': '/home/bier/html/static'}),
I have more simpler solution. you need to create a directory named, let's say 'x' in "public_html" or similar location from which server serves the files by default.
Then upload all static files in directory x. (this can be done by running collectstatic locally and then upload all contents of directory STATIC_ROOT to x)
Then, change your STATIC_URL and STATIC_ROOT as follows:
STATIC_URL = '/x/'
STATIC_ROOT = os.path.join(BASE_DIR, '../public_html/x') # Path to folder

Django admin, static files use wrong directory

I am using django 1.4, and when I have this in settings:
STATIC_URL = '/_s/'
STATICFILES_DIRS = (
('admin', '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin'),
)
And then access /_s/admin/css/base.css Django tries to load it from a different path and writes:
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/css/base.css" does not exist
Why does this happens?
UPD
I use ./manage.py runcserver 0.0.0.0:8001
The STATICFILES_DIRS is used for collecting static files, ./manage.py collectstatic. It tell Django which folders to look in in order to pull all static files together in to one place.
When serving up static files Django will only look in STATIC_ROOT.
Official docs
The problem was in
./manage.py runcserver 0.0.0.0:8001
I used concurrent server, not default django debug server (I thought they both work in the same way (except threads))
But it is not true =(
UPD
From 1.4 you can use django debug server, it is multithreaded by default.