After doing a bit of googling, I found these projects to help with serving static files:django-static with Nginx, and django-staticfiles.
Is there anybody that has had experience with one, or preferably both of these approaches, and that can recommend one or the other, or a 3rd?
The usual way to handle static files is actually not sending them through django, but let the web server (e.g. apache or ngingx) handle them. I provide a small example for mod_wsgi, based on official django docs, found here.
Alias /media/ /usr/local/wsgi/static/media/
<Directory /usr/local/wsgi/static>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /usr/local/wsgi/scripts/django.wsgi
<Directory /usr/local/wsgi/scripts>
Order allow,deny
Allow from all
</Directory>
The first statement makes sure all files in /media will be served through apache directly, not django. The second statement is for setting up the django site itself. Now, using this media files do not go through django processing, which is often painfully slow.
The reason static file servers exist is mainly for development or very minimalistic rollouts.
Related
So I am converting the Django tutorial to fully work with Apache instead of using the built-in "runserver" command. I got step one working; getting Apache to serve the static files (css). Now I need to get it to serve the static files for the admin.
My code so far in the httpd.conf file for Apache 2.4.
#static files for site
Alias /static/ "C:/mysite/polls/static/"
<Directory "C:/mysite/polls/static">
Require all granted
</Directory>
#static files for admin
Alias /static/ "C:/Python27/Lib/site-packages/django/contrib/admin/static/"
<Directory "C:/Python27/Lib/site-packages/django/contrib/admin/static">
Require all granted
</Directory>
Obviously having the same alias for the 2nd block does not work and the CSS will not load for the admin. The site (1st block) loads fine. Now this page in the Django tutorial details the entire process on how to make it work. I just cannot figure it out. Maybe I am doing a syntax error and I have read countless posts about this both here and elsewhere.
The doc mentions 3 ways to do it. I want to do it the 2nd way; by way of using the alias directive: "Use an Alias directive, as demonstrated above, to alias the appropriate URL (probably STATIC_URL + admin/) to the actual location of the admin files."
Now I don't understand the exact part where it says "STATIC_URL + admin/). I tried various variations of that but it won't work. My link to the admin page is exactly this:
http://127.0.0.1/admin/
Can we figure this out in specific to WINDOWS and DJANGO 1.6? I know prior to DJANGO 1.4 there was a different way using "ADMIN_MEDIA_PREFIX" in the settings.py file. That way is deprecated now and I want to use the alias. And bonus. How do aliases exactly work?
Thanks all.
EDIT:
Link that mentions how to do it. Under "Serving the admin files" I need to figure out the 2nd way using alias.
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
This is what the "static" app is for. You should do manage.py collectstatic on deployment, and this collects all your static content - both for your apps and for the built-in/contrib ones - into one place, which is where you point your Apache alias to.
But if you really want to hard code it, STATIC_URL + admin just means exactly that: the value of STATIC_URL, suffixed with "admin", so Alias /static/admin.
This is the code that finally works:
Alias /static/admin "C:/Python27/Lib/site-packages/django/contrib/admin/static/admin/"
<Directory "C:/Python27/Lib/site-packages/django/contrib/admin/static/admin">
Require all granted
</Directory>
Alias /static/ "C:/mysite/polls/static/"
<Directory "C:/mysite/polls/static">
Require all granted
</Directory>
Note that ORDER matters greatly. I have to Alias the admin static BEFORE aliasing the site static. Sounds like a cascading type issue and makes sense the more specific gets precedent.
Also I had my link incorrect for the admin. It was ending in ../admin/static/. It should go deeper into ../admin/static/admin/.
Finally 2 areas working to serve static files. The admin comes first and then the site static 2nd.
Thanks to all and this should be really documented and might be a pitfall for some.
I'm new to Django and have a question about Django application design.
I want to write one application and have multiple instances of it running, each under a different URL. Each instance would store its data in its own tables and each instance would have a different set of users. The different instances would be accessed from different URLs, but from the same domain and run the same application code behind the scenes.
For example if I had two instances, they would be accessed from the following URLs:
http://www.domain.com/instance1
http://www.domain.com/instance2
I've got my models defined, and I can run a single application, single instance, with multiple users just fine. However it's not clear to me how to support multiple instances.
I've taken a look at the sites framework, which is almost what I want, but I need everything to be hosted under the same domain.
Any tips on how I can run multiple instances of a single application with Django?
You could simply copy the django project to another place and create a new apache site.
For example:
Create a virtual host in /etc/apache2/sites-available/domain.eu_first
<VirtualHost *:80>
ServerName domain.eu
WSGIScriptAlias /first /var/www/first/wsgi
<Directory /var/www/first>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
and
a virtual host in /etc/apache2/sites-available/domain.eu_second
<VirtualHost *:80>
ServerName domain.eu
WSGIScriptAlias /second /var/www/second/wsgi
<Directory /var/www/second>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now copy your project to first and second. And activate your apache sites by a2ensite domain.eu_first and a2ensite domain.eu_second
You have to proof that you have used only relativ links in your project.
Answer is unverified so just check it out.
Regarding this documentation page from the Django website,
https://docs.djangoproject.com/en/1.2/howto/static-files/
where it says for development "With that said, Django does support static files during development. You can use the django.views.static.serve() view to serve media files."
So my question is, if I use this method, How much work is required to move to apache.
Currently I have a symbolic link to my image folder in the /var/www folder, and in the Django settings I have set the media url to :
MEDIA_URL = 'http://127.0.0.1:80/Images/'
This seems like a fairly easy hack but my project is going to get very big (with lots of css, js and pdfs) and I doubt if this method is good.
My approach was to have apache itself intercept the static files urls and serve them directly without invoking django at all. So my apache config looked something like this:
<VirtualHost *:80>
ServerName www.myproject.com
Alias /static /docs/my_website/static
<Directory /docs/my_website/static>
Order allow,deny
Allow from all
</Directory>
Alias /favicon.ico /docs/my_website/static/images/icons/favicon.ico
Include "/13parsecs/conf/django.conf"
</VirtualHost>
Then you can just keep doing whatever you're doing in the dev environment, and when you get to apache it won't invoke django at all for static content, which is what you want.
This is a perfectly good way of doing things. The only change you'll need to make is to put the actual URL of your site, rather than the localhost IP.
Don't "move to Apache", start using it in the first place. None of the software needed has licensing fees and runs on almost any platform, so the only excuse you could have is "I'm too lazy".
I would like to have multiple django projects living at the same root url like this:
example.com/ # controlled by home django project
example.com/project-2 # controlled by a separate django project
example.com/project-3 # controlled by yet another django project
I am already redefining the LOGIN_REDIRECT_URL, etc. as suggested by this excellent answer, but I have discovered another hurdle. I am actually using the same apps in the projects that live at example.com/project-2 and example.com/project-3, which causes some non-trivial problems for linking to content inside of a django project that have thus far been solved with seemingly hacky solutions.
For example, you can never refer to '/' in any template in either example.com/project-2 or example.com/project-3 to return to the root of the django project hosted at either of these URLs --- this will link to the home django project at example.com. To get around this, I have made a context processor that correctly prepends the root url of the project based on a custom settings.py variable SCRIPT_NAME: '' (for example.com), '/project-2' (for example.com/project-2), or '/project-3' (for example.com/project-3). This is all fine and good except that you need to do the same thing in the get_absolute_url functions. Before I knew it, I had just turned a bunch of code that was very reusable (by people other than myself) into code that was not reusable at all.
Is there a way to accomplish the same effect without having to prepend absolute URLs with the SCRIPT_NAME? Perhaps something clever with apache or mod_wsgi configuration? I am at a loss and hoping someone can help...
EDIT:
My apache configuration for example.com looks like this:
# redirect un-'/'-terminated urls to the '/'-terminated root urls
RewriteEngine On
RewriteRule /project-2$ /project-2/ [R=302,L]
RewriteRule /project-3$ /project-3/ [R=302,L]
# mod wsgi setup
WSGIScriptAlias /project-2 /srv/project2/project-2.wsgi
WSGIScriptAlias /project-3 /srv/project3/project-3.wsgi
WSGIScriptAlias / /srv/project1/project-1.wsgi
You don't show how you're serving these projects from your Apache configuration, which would have been useful. But if you define them as separate WSGIScriptAlias directives, then SCRIPT_NAME is automatically passed through for you, and Django takes it into account when reversing and creating URLs.
WSGIScriptAlias /project-2 /srv/project2/project2.wsgi
WSGIScriptAlias /project-3 /srv/project3/project3.wsgi
WSGIScriptAlias / /srv/project1/project1.wsgi
I have a shared hosting account for my ColdFusion websites. One of my customers needs CFChart graphics for his statistics. I've programmed them and they run ok on my own development server, but they don't show up online. The reason is that ColdFusion puts the generated images into /CFIDE which is outside of my part of the file system, and not accessible for me in a shared hosting environment.
IMG SRC="/CFIDE/GraphData.cfm?graphCache=wc50&graphID=Images/4990209100100002.PNG"
The hoster uses IIS on a Windows machine and CF7. He has tried several things (configuration-wise), but so far, nothing helped.
What can we do?
We have a site that creates statistical charts on a schedule. CFChart allows you to store the data to a variable (the "name" attribute). Then use CFFile to write the chart to any location within your webroot. We use it for Flash charts, but I've tested it with PNG as well, and it works fine.
I'm not sure how you'd go about adding this to IIS, but, I've used this on apache to solve the same issue:
Alias /CFIDE /var/www/html/CFIDE
<Directory /var/www/html/CFIDE>
Order deny,allow
Deny from all
</Directory>
<Files ~ "^GraphData.cfm$">
Order allow,deny
Allow from all
</Files>
I believe it would be possible to use the techniques described in this blog post:
link text
And store the image in a location where the browser could get to it.