DJango, use Javascript provided by OS package manager during development - django

I'm preparing a package developed in-house to be delivered to a customer. During development in house, we've used copies of common JavaScript libraries (jquery, openlayers, underscore) in our code repository, and that works. However, in the delivered system, management feels (and I agree) that if we deliver our 'own copies' of these common libraries then we become responsible for them.
Consequently I'm trying to rework the system so that it serves these libraries from /usr/share/javascript, which is where the OS (Ubuntu) puts them. This works fine when the package is deployed with Apache; however it doesn't work when run from
python manage.py runserver
What do I need to add to my settings.py so that runserver will serve files from /usr/share/javascript on the URL
http://localhost:8000/javascript/ ?
How do I ensure that those files are not collected by 'collectstatic'?
Thanks!

After a lot of messing about, I have a solution; but it's complicated and ugly. It wasn't possible to make runserver emulate the apache environment, or apache emulate the runserver environment. So to make both Apache and runserver work with package-manager provided javascript libraries, the following key changes are required:
Add /usr/share to STATICFILES_DIRS in settings.py, but
remove it before running collectstatic
add Options FollowSymLinks to the apache configuration for /var/www/static, and
symlink /usr/share/javascript into /var/www/static
So, for clarity, the Apache configuration contains
Alias /static/ /var/www/static/
<Directory /var/www/static>
Options FollowSymLinks
Order deny, allow
Allow from all
</Directory>
settings.py contains
STATIC_ROOT = os.path.join(BASE_DIR, '/myapp/static')
STATICFILES_DIRS = (
'/usr/share',
)
STATIC_URL = '/static/'
Before running collectstatic, /usr/share is REMOVED from settings.py, otherwise you collect a whole lot of things you don't want to collect.
I'm not saying this is an ideal solution, but it's working.

Related

All my static files in Django are not found, but findstatic finds them

I am developing a Django site on an Ubuntu 16.04 LVM using Apache and mod_wgsi. When I try to load one of my pages, all the static files give a 404 not found error. I believe I have my settings set correctly because
python3 manage.py findstatic
is able to find the files and running the development server with DEBUG=off and the --insecure flag, It still works correctly. Does this mean it is an issue with apache?
Here are my static file settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
#'/home/matt/jukebox/bin/jukebox/static/'
)
Does anybody know what could be wrong? The files were being served previously, but after updating my server and the code for the site, it stopped serving.
Thank You
Adding this to my apache config file fixed the issue:
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
I found the answer here:
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04
Still not sure why it had been working previously without these settings, but hopefully this answer can help someone.

Django Admin Static Files on Apache

There are quite a few posts on here regarding the same subject, I've tried as many as I thought applicable. Yet, I still have this error. Everything is being served fine for a django site I've built except the static files for admin.
Versions: Django 1.6, Apache 2.4.7, Ubuntu 14.04
Ok - I have ran manage.py collectstatic and here is the static folder:
/srv/bahai-site/bahai-site/soul/static
This contains four folders: 1) admin/ 2) bootstrap/ 3) core/ and 4) flags/
Within these folders, I can see all expected static files and that they contain the code I'd expect.
When looking at a static file on the site itself, something like /static/core/css/desktop_992.css loads correctly while /static/admin/css/base.css throws a 404.
Apache directives in http.conf:
Alias /static/ /srv/bahai-site/bahai-site/soul/static/
<Directory /srv/bahai-site/bahai-site/soul/static/>
Allow from all
</Directory>
WSGIScriptAlias / /srv/bahai-site/soul/soul/wsgi.py
WSGIPythonPath /srv/bahai-site/soul/soul:/srv/envelope/bahai-site/lib/python2.7/site-packages
<Directory /srv/bahai-site/soul/soul>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
Side question: notice the WSGIScriptAlias and WSGIPythonPath only contains one "bahai-site/" directory level, instead of the two that actually exist after "/srv/". Why is the site running fine? Shouldn't that be causing a problem?
Anyway, what am I doing wrong here? How do I get these admin static files up and running?
as far as the pythonpath is concerned I personally don't include that in my apache virtual host config file and it works. Maybe that's not needed? Honestly I'm not all that well versed in server installations. It defintely seems like a permission issue.
I would run collectstatic using sudo, then I would also make sure the static folder has www-data group permission which is the permission apache uses out of the box.
If you need help setting permissions on a folder in Ubuntu check out this link here. https://askubuntu.com/questions/244406/how-do-i-give-www-data-user-to-a-folder-in-my-home-folder
The problem was that I had updated two different configuration files for Apache with competing directives.
Note to Other Beginners:
Directives (i.e. things like ...) can be placed in any file in the apache2 root -- in my case that was any file within /etc/apache2/. (Question to advanced users: is this only for Ubuntu? Or pretty much any Linux/Unix?) This will confuse you because, if you solve a problem using information from multiple online posts/documents, you'll likely have a smorgasbord of various people's Apache configuration preferences/styles. Hence my problem.
As a beginner, you're probably expecting to see some kind of single location you can put your configurations/customizations (in this case directives) into. That's not the case on Apache. You can put any of these anywhere. This is probably good once you advance your knowledge of Apache but it sucks when you're starting out.
My fix here was to go through all the files I touched in the /etc/apache2/ folder and consolidate any rules into one place. This lead to me removing duplicate/conflicting rules, which solved the problem.
Now I think I need to talk to a therapist....

403 not found in EC2 - apache&WSGI error

I have editted the httpd.conf inside apache by adding:
<Directory /home/ec2-user/hqlocal>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/ec2-user/hqlocal/hq_local/apache/django.wsgi
WSGIPythonPath /usr/lib/python2.7/site-packages
# Serving Django admin static files
Alias /static/admin /usr/lib/python2.7/site-packages/django/contrib/admin/static/admin
# Serving Django static files (our own files use in our app), assuming in settings.py, STATIC_ROOT is “/var/www/static/” and our STATIC_URL is “/static/”
Alias /static /var/www/static/
I have tried many solutions that fix the 403 problem like swapping deny and allow, and so on. But none of them works, I still get 403 cannot access '/'
Therefore, I tried commenting line by line.
The result is that the line
WSGIScriptAlias / /home/ec2-user/hqlocal/hq_local/apache/django.wsgi
causing the problem.
By adding this line, the system becomes 403 but without it, when entering the public DNS, the server will point to Apache default page.
So, I assume that Apache may not have permission to read this file. Therefore, i changed the permission to be global. However, it still didn't work properly.
May I know how to fix this?
Thank you very much.
The directory '/home/ec2-user' is likely not readable to the Apache user. Watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
for common mod_wsgi setup issues including this one.
Also, you should not need:
WSGIPythonPath /usr/lib/python2.7/site-packages
if mod_wsgi is actually compiled against the system Python 2.7. If it isn't compiled against the system Python 2.7, but another version, you should not be forcing it to use modules installed into another Python version as that will cause problems. You should reinstall mod_wsgi with version compiled against correct Python version you want to use.
There are two possible reasons.
Make sure you set the permission correctly for the directory '/home/ec2-user/hqlocal', you can do this:
chmod -R 755 /home/ec2-user/hqlocal
Disable selinux (if using enforcing mode, you need get permissions for apache in selinux). You can configure the /etc/selinux/config:
SELINUX:disabled
Then reboot the machine.

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

How to setup Django/Apache for a designer's dev environment

I've been developing in my own django environment for a while now using the manage.py runserver with no problems, but now that we've got a designer and a front-end developer needing to work on the project, I find myself at a loss as to what is the Best Practise for their environments.
I could ask them to setup their own python environment, but that's asking an awful lot since they're not Python people and they're running Windows (my dev and the production environment are both Linux).
So instead, I've set them up on a remote server, the disk of which they can mount locally. However in this setup, I'm actually using different instances of manage.py runserver ip:port running in a screen instance. It doesn't handle things like constant reloads very well (common for our designer) and it hangs from time to time due to the single-threaded nature of the dev server. I'd like to know how to set this up with Apache.
The problem with this of course is the staticfiles. Every time either of the aforementioned parties want to add or change a static file, they'd have to run manage.py collectstatic which just isn't practical. I just don't know any other way to do it though. All of the documentation I've found for using Apache is for a production environment, so... that's why I'm here.
Source control? Have them check in changes and then set up a post commit hook to collectstatic and restart the server. With nice windows GUIs I've never had a designer who couldn't grasp the basic concepts. If you're using a dcvs you can always have them in their own fork so you have to merge into the main repos to prevent them from breaking other things by mistake.
The answer to this one was a lot simpler than I thought it would be and I apologise for confusing those who responded. Basically all I wanted was a way to host our designer's dev environment in something more stable than ./manage.py runserver ip:port in a screen session. I figured that there had to be a way to set something like this up for Apache but had no idea what it was.
Here's what I got to work:
In your settings.py set your STATIC_URL and MEDIA_URL variables to relative URLs. In my case I used /static/ and /media/.
MEDIA_ROOT = PROJECT_ROOT + "/htdocs/media/"
MEDIA_URL = "/media/"
SERVE_STATIC = True
STATIC_ROOT = PROJECT_ROOT + "/htdocs/public/"
STATIC_URL = "/static/"
Configure Apache as you would if you didn't have any static files at all. In other words, ignore the recommendations of the docs to use SetHandler None in a <Locaiton> block.
<VirtualHost *:80>
WSGIScriptReloading On
WSGIDaemonProcess someprocessname
WSGIProcessGroup somegroupname
WSGIApplicationGroup somegroupname
WSGIPassAuthorization On
WSGIScriptAlias / /path/to/config.wsgi
ServerName somewhere.awesome.ca
<Location "/">
Order Allow,Deny
Allow from all
</Location>
ErrorLog /var/log/apache2/somewhere.awesome.ca.err
CustomLog /var/log/apache2/somewhere.awesome.ca.log combined
</VirtualHost>
Lastly, you just have to follow the Django howto for serving staticfiles through Python at https://docs.djangoproject.com/en/1.3/howto/static-files/#serving-other-directories
I hope this helps to point someone in the right direction in the future.