Django - Loading gzip files only in production - python

I am currently serving all of my static files through S3. I have a handy script that I wrote that will allow me to sync all my files up to S3. It will even gzip them before sending them along their way, changing their extensions all to .gz.
Here's the issue. Right now, when I want to load for example, my code looks like this:
<link href="{% static "sparestub/css/jasny-bootstrap.css" %}" rel="stylesheet">
Simple enough. On my local machine {% static %} pulls in a path to the local file system, and on my production machine, {% static %} pulls in a path to S3. But the moment I gzip all of the files in S3, changing all their extensions to .gz, {% static %} is going to return an invalid path containing the original extension.
What is the typical solution for this type of problem? Modify {% static %}? Is there another library that can help me out?
EDIT:
Or should I really just be gzipping the files and not changing the extension?
EDIT 2: This post here (Can someone walk me through serving gzipped files from Cloudfront via S3 origin?) suggests that I just gzip the files without renaming. So I did that. I can view the files in the S3 bucket using Amazon's web interface and can verify that they were successfully gzipped by looking at the HTTP header. The thing is now my app gets 403 errors whenever I try to load the files. argg.

Related

How should I manage my images for my django app

Here I am once again asking a question that might seem stupid:
I am new to web development. So far I've made some practice projects with Django and I've deployed two projects so far as github pages but they were simple static websites (no django).
Now I'm making a web application for my mom's business with Django.
I don't know what I should do with all the images I need for the website.
In the past I have uploaded all jpg and png images to imgur and copied the link in my static websites html
But now I have more images and I am going to pay for a server for them application. I have two types of images:
Images that will always appear on the page. Because that's part of the design
Images that the admin can upload when creating a new post for the page
So my question is:
Should I have all images in the statics folder for my application?
And call them like this:
<img src="{% static 'website/images/home-slider/myimage.css' %}">
And for the other images should I save a similar path in the database?
I just have no idea if things should be done different for production
Images used as a part of html files are served within static files and images uploaded by the user as a part of creating new post are saved as media files. You usually have seperate static and media root for saving files to avoid confusion.
Media files are served as '/media' as static are served as '/static'.

cant load static file in django

I copied a HTML page with many images and javascript codes.
I created the static folder and did everything that is needed, and when I'm tying to load the source with static tag such as:
<link rel="stylesheet" type="text/css" href="{% static 'styles/bootstrap4/bootstrap.min.css' %}">
I'm still getting an error:
GET http://127.0.0.1:8000/static/styles/bootstrap4/bootstrap.min.css net::ERR_ABORTED 404 (Not Found)
All it did is to add static in the url and I dont understand why?
I cant find how to load jinja in pycharm so my tags are white.
Have you looked at the Django Documentation?
Serving the files
In addition to these configuration steps, you’ll also need to actually serve the static files.
During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).
This method is grossly inefficient and probably insecure, so it is unsuitable for production.
See Deploying static files for proper strategies to serve static files in production environments.

Setting my <img> source to a file path outside of flask static folder

I need to display some images and these image filepaths are outside of flask's static folder stored on on network folders.
<img src="{{ filepath }}"> # /some/external/network/folder/img.jpg
However this is not working and I get a broken image icon, however I can paste the image path into my browser and confirm its accessible. I need this application to be able to accept image sources from anywhere, so I can't update my static folder to the network folder. I also can't load the images to the server because there would be about 20,000+ photos.
Is there a way to get this to work?

My Flask app is not properly serving videos on raspberry, but normally on my laptop

I've got flask server for streaming movies. Everything works fine while server runs on normal computer (laptop with arch) but when raspberry serves the video (using flask's static folder) I'm unable to skip (forced to watch sequentially), so when I close browser in middle of movie I have to watch it all over again, that's pretty annoying.
I'm using just <video> tag with <source> linked to static .mp4 file served by flask.
I don't know if this could be cause by just fact that raspberry cannot stream full HD video properly, but when I'm watching sequentially I have no issues.
Whole project is on GitHub so If you want to look at the code:
"movie player" template is here
code that generates that "movie player" html is here
Thanks for any suggestions.
PS: I have got rapsberry 3 model B and movies are stored on 4TB NTFS HDD.
EDIT: Tried 8GB Fat32 USB stick and still same problem.
Requested minimal working example:
HTML - part of video_player_main.html
<video controls="controls" autoplay="autoplay" style="margin: auto; display: block;">
<source src="{{ url_for("static", filename="movies/%s" % filename) }}" type="video/mp4">
Get better browser!!!
</video>
Flask generating website
#video_player.route("/play/<path:movie>")
def play(movie):
return render_template("video_player_main.html", filename=movie)
HDD with movies is mounted on ./static/movies/
What MIME type is being returned by the Flask server for the static movie files in each case? Use the wget command (or something similar) to see what's being returned.
You are likely getting different MIME types on the different systems, which is causing your browser to handle the video streams slightly differently. Note that the browser doesn't care about the file extension; it goes by the MIME type returned by the server.
Flask uses the mimetype Python library to figure out what MIME type to use, based on the file extension. It uses a bunch of local files to make guesses, see: https://github.com/python/cpython/blob/2.7/Lib/mimetypes.py#L40

Implementing HTML & CSS into Django

I'm trying to make my views.py point to a HTML page I've made that has embedded CSS, is this the best approach? I'm also running Django locally for testing purposes until it is moved to a production server how would I make local links to point to my HTML ?
https://docs.djangoproject.com/en/1.10/ref/settings/#templates
The default includes app/templates/ to your template paths and i would recommend to store your template files there.
Static files like .css and .js are served in similiar way
https://docs.djangoproject.com/en/1.10/howto/static-files/
The django test server also serves static files like css and js for you.
I would also recommend to move your css in one or more seperate .css files.
It helps you on the long run to keep your project clean.
1) In your views.py should be something like:
def my_view(request):
return render(request, 'my_html_page.html', {})
2) You should put 'my_html_page.html' to folder "templates/"
3) You should make folder "static" and put there you css file (i.e. styles.css)
4) in "my_html_page.html" you should link your css like this:
<link rel="stylesheet" href="***{% static 'styles.css' %}***" rel="stylesheet">
5) you should run command
python manage.py collectstatic
p.s. on production server you should install whitehoise
pip instal whitenoise

Categories