django css and js files gzip compression on heroku - python

I am trying to compress my css and js files and serve as gzip file, but with no success.
I have tried all methods, django-pipeline, django-compressor etc.
I got django-compressor to create the gzip files but I am not sure how to serve them. I use GzipMiddleaware to compress and serve my html files.
Can any one please write step by step guide on how gzip and serve static files on heroku django.
Sorry to ask for detailed steps, I am not asking to be spoon fed, but am stuck with this issue for almost 24 hours now, tried n number of solutions, but none seems to work.
FYI, I am using django-1.5
Thanks in advance for your help

You should seriously consider uploading your static files to s3 as well. Django is really not cut out for serving files, and it will consume precious resources on your app server to do so.
Here's some great reasons for using s3 for staticfiles. http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/
Here's a article explaining how to get S3 to serve gzipped resources: http://www.jamiebegin.com/serving-compressed-gzipped-static-files-from-amazon-s3-or-cloudfront/
Here's a storage backend that will collectstatic to s3.
You could probably easily write a storage backend that gzips and upload to s3 by subclassing this backend and django-compressor's backend.

I had the exact same issue. I was using grunt but found there was a lot of extra config etc etc etc then I found django-compressor which is AWESOME!
But I deploy to Heroku which doesn't gzip anything and relys on the app to do it.
I couldnt find any wsgi wrapper that served pre-compressed gzip files, so I extended dj-static to do it for me. Check out my fork of dj-static which includes the additions. Basically it will check for a corresponding static filename that ends in .gz and if it finds it, it will serve it instead. I use this for my Heroku projects.
https://github.com/hdickson/dj-static
To test on your dev server if you run the django built in web server be sure to run it with the --nostatic command line switch. ie python manage.py runserver 0.0.0.0:80 --nostatic

Related

Static file storage and server hosting - Django

I am new to server side operations and have a personal project that I am building. I am running Python3, Django2 with mysql (which will be changed to postgres for live usage) and currently have my static files stored inside the virtualenv on my local drive. It's also built locally on Ubuntu 16.04 if this changes anything, for example: Maybe git deployment would be easiest?
My first question is: I don't expect lot's of traffic (under 1000 a day), and databases are very simple. Only an admin can upload static content, such as posts, images, tags and categories. Essentially a blog format. Where should I store these files? Amazon s3, azure, google, or anything you suggest
2. Second question is: Where should I host my web app, and how will it affect where I store my static files?
I'd like to note that I am an entrepreneur doing this on my own so an inexpensive and simple setup is what I am aiming for as I don't have much experience in server side tech, but willing to learn.
Thank you in advance for your time and sorry if I left out some required information, I'll update the Q as needed.
We store our static files on s3 using s3utils. You should also check out the this post, it describes how to set up an S3 bucket with the proper permissions.
We host our webapps on AWS Ec2 with Nginx and Gunicorn. Check out How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04

What are important tricks about saving heavy media files in production?

What should I know about saving files in production? I need to save lots media which are pdf files. The only thing I know so far, is that I shall rename pdf files into my own naming system (for example by overwriting storage in django). But what else is important ?
Or that's all, just saving all files like 1.pdf,2.pdf,3.pdf,4.pdf.. in one media folder and it will work in long term without any other tricks and optimization? I am using django1.8 and python 2.7, but I guess it's very general question regarding running production server at all .
I hope it's not off topic, as far I faced the lack of information on the issue.
Here are some tips:
Follow a directory structure similar to this: appname/%Y/%m/%d.
Use AWS S3, or another storage service, for your media and static files.
Compress files to reduce size.
Use whitenoise, or a similar package, to serve static files in production.
Note: These tips are not solely for media, but also static files.

An easy way to show images in Django on deployment (DEBUG=false)

I am using DJango 1.8 and python 3.4.3, and I have been running my app on Debug mode, and found a way to show images inside a directory configured on MEDIA_ROOT, this was my first question and the solution I have found: How to upload and show images in DJango. But reading the docs I found that that solution is not suitable for a served app, so, if I stop using "Debug=True" the images will not be displayed, and I have to use one of the options exposed on this link: Static files on deployment but I don't have money to pay another server, I just can pay my hosting on pythonanywhere, and for the option to use the same server for the images, I don't have idea how to automate the collectstatic and also don't know how to trigger it when an user uploads a new image.
I have used ASP.NET and PHP5 and I didn't had problems with the images in none of them, so, I have two questions:
Is there an easy way to show images URL's?
Is there a high risk security problem if I deploy my app with DEBUG=True?
I hope you can help me, because I find this ridiculous, probably is for better security, but it just not make sense for a framework, instead of making the work easier it make it more difficult and stressful.
Django runserver is not intended for serving up static files in a production environment. It should be limited to development and testing environments.
If you are intending to use django's runserver to server up static files with DEBUG=False then use the --insecure flag.
You should never deploy a site with DEBUG = True due to security implications.
Static files and media assets are 2 different things.
Static Files
Static files are things like images you created and files that come with 3rd party apps you have installed (e.g. django-cms). These files include images, css and javascript files etc.). So you need to have a settings.STATIC_ROOT for this.
python manage.py collectstatic collects static files from different locations and puts them all in a single folder.
Media Files
Media files are things the user uploads (e.g. photos, documents etc.). So you have a settings.MEDIA_ROOT for this. collecstatic won't do anything to media files, they will just be there already once the user uploads them.
Serving up static and media files in production
Frameworks like Django aren't going to cover automatic production server configuration - that is something else you will have to learn unfortunately.
There are a lot of good guides around e.g. this one to help you get started serving media and static files in production.
Regarding server costs, I'm sure you can find a host to give you some free credit, or pay $5/month for a server somewhere... try lowendbox
Here is a guide from pythonanywhere regarding media and static files: https://help.pythonanywhere.com/pages/DjangoStaticFiles/
1) in urls.py add:
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
and open url http://myhost.com/media/
2) Never deploy a site into production with DEBUG turned on, DEBUG=True is a security issue,

Pyramid server not serving flash files

I am running this python pyramid server. Strangely, when I moved my server code to a different machine, pserve stopped serving flash videos in my static folder. Whereas it serves other static files, like images, fine ! What could be a reason for this ?
Pyramid's static views serve up files with a content-type determined via python's mimetypes module. Most likely you'll need to add support for the extra video types to your installation at startup. Specifically something along the lines of the following, somewhere in your code.
import mimetypes
mimetypes.init()
mimetypes.add_type(...)
I possibly ran into a similar problem on my pyramid app. I'm using TinyMCE and had placed the files in the static folder. Everything worked on my dev server, but moved to test and prod and static .html files related to TinyMCE couldn't be found.
My web host had me add a symlink basically I think hardcoding to the server software (nginix in this case) the web address to my static HTML to the server path and that worked.
I'll have to check out the mimetypes thing, though, too.

Can I deploy Python .pyc files only to Google App Engine?

I'm working on a project utilizing Django on Google App Engine. I've been asked if some of the code can be deployed as compiled only.
So I guess the question is can I upload a .pyc file only that contains the piece of code in question? I've done a basic test with a views.pyc file in an application and things don't work. Is there some configuration or other that I can set to allow Google App Engine to just use the .pyc files?
No, you can't - you can only upload sourcecode. There's no good reason to do this, though: your code will be bytecode-compiled on the servers when needed, and nobody is able to access your code in any case.
I realise you couldn't do this when you asked this question, but you can now, if you use Python 2.7. See Sandboxing in Python 2.7:
You can upload and use .pyc, but not in combination with .py files. You can upload zip files containing .py or .pyc files (or a combination).
Why would you want to do that in the first place? Because your .py files are uploaded to Google infrastructure and can be seen only if you explicitly give permissions.
But yes, there is no reason as why uploading only .pyc files should not work. If you try it, in your dev environment, you will find it working just as BaseHTTPServer can take python compiled modules as handlers.
Also, recent GAE supports automatic precompilation for python files, which means that as soon as you update your application, the python files can precompiled and served. So, you might have to play with --no_precompilation during appcfg.py upload if there was any expectation to check for .py files at the app engine end.

Categories

Resources