django static files not served to HTML-EMail template - django

I am running Django on a local Vagrant arch machine on a Win7 host. I set up my environment variables from Django in a .env file. In my app all static files are served correctly and everything works as it should.
Problem: I am not able to serve my static files (images) in my html-email templates. Until now i served them as hardcoded filer URL's and i want to change that.
I am passing BASE_URL BASE_URL=http://127.0.0.1:8001, which is proved working, as context to the template and loading static as usual:
{% load static %}
and calling it in HTML tag:
<img src="{{BASE_URL}}{% static 'img/my_image.png' %}">
In the received email the URL of the image is http://127.0.0.1:8001/static/img/my_image.png which looks right but triggers a 404.
What am i missing??
(Please dont ask me if the image is in the corresponding folder, it is ;)

As already said by #dentemm: Your email service provider try to fetch images from http://127.0.0.1:8001/static/img/my_image.png but he cannot while address of your server is visible only from your local computer. Therefore images are not found. One way to solve this is to render template and take screenshot of template and send that in email body(to see if it renders properly) E.g. here.
Another way is to upload img files to some publicly accessible server.

Related

How to rename "admin" static files in Django?

When loading up the Django admin pages, you load in the default Django admin css / js files. For example, example.com/staticfiles/admin/css/base.css.
I've enabled a default Amazon WAF (firewall) rule that blocks all access to pages that have the word "admin" in their URL. This has been great so far -- as most bots love looking for /admin domains.
However, this ends up blocking the Django admin. I'm able to rename the Django admin url to /manager using one line of code in urls.py, however I can't figure out how to to change the admin static files' filepath. I'm currently getting raw, unstyled HTML in my admin as all the static files are blocked.
Ideally, in settings.py I could write:
ADMIN_FOLDER_NAME = 'manager'
and then
example.com/staticfiles/admin/css/base.css would get rewritten to example.com/staticfiles/manager/css/base.css.
How can I tell Django to look in a different folder for admin files?
Note
I am not asking how to rename the url for accessing the admin page. I am talking about the admin static files. The below code does change the admin page url, but does not change the url for the staticfiles.
urlpatterns += [url(r'^newadminurl', admin.site.urls)]
As you may have noticed in example.com/staticfiles/admin/css/base.css, the url is based off of the folder structure.
Also, it should be noted that in django admin templates, static file paths are said to look into a folder named admin like so: {% static "admin/css/base.css" %} (admin static file paths are hard-coded).
# a line from admin/base.html
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
This means that you cannot change the path after example.com/staticfiles/ for admin static files.
But, can be done by copying the templates directory
Copy templates directory from venv/Lib/site-packages/django/contrib/admin/ to project base folder
Find all the occurrences of {% static "admin/ and replace {% static "admin/ with {% static "manager/ in the templates. Need to be careful not to change any other thing in the template files.
Update TEMPLATES settings > 'DIRS': [os.path.join(BASE_DIR, 'templates')]
Overriding Admin Templates
Another possible way to achieve this could be rewriting the URL to remove or replace admin from the URL. Simply rewriting the URL would result in 404. Furthermore, server configuration for serving the admin static files has to be additionally added.
You're solving the wrong problem. Firewalls have standard rules to get you started, but must be customized to match the environment they're protecting, not the other way around.
Renaming files and routes each time you install or upgrade something is time consuming and error prone. Make exceptions for routes that exist - they are valid routes, so teach the WAF about them.
Just think about it this way: you would never post a question like this:
"I've just bought a firewall. What application framework can I use that can be put behind it, without needing to change the framework?"

Displaying an image with an absolute path on server

Suppose I have an image on the server machine (Django on Windows) at C:/myimage.jpg. I want to display this image on the client machine. The image path is passed to a template as a context variable image_path. It is important that the path is absolute and not relative. Here is my attempt on the template:
<img src="{{ image_path }}" />
However, the client does not see an image at all. My first guess is that when it reads image_path, it actually reads C:/myimage.jpg, and therefore looks on the client machine, rather than the server. How can I make it look for the image on the server instead?
Using absolute paths for static content within the app is not a correct idea. You should use images using the following method,
add this in your settings.py
STATIC_URL = '/static/'
and then accessing the image using
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
please read the guidelines for more details
Even if you could tell a browser to look in a specific file location on the server, how could that ever be a good idea? What if the user entered the path to the settings.py file containing your database credentials?
Clearly, you can only look for files via URLs, and those URLs actually have to be mapped to actual files by the server. So you need to configure your web server in such a way that it maps a URL to the place your files are, and then you can use that URL in your template.

Django static content through nfs on an intranet

I'm deploying a Django application that is based on a whole lot of static content. All of the computers using the application are on an intranet, with the static content available through nfs.
Can django be configured to let users get the static data through their nfs mounts, rather than forcing it all through a web server?
If I've got this in a template:
<img src="/path/to/img.png"/>
The browser is requesting that image from
http://localhost:8000/path/to/img.png
What I'm trying to do is get the client to treat that as a local path, rather than asking a server for it.
To reference a file in your file system (NFS mounted or otherwise), the URI to use is file:///path/to/file. However, that will not work in your case -- <a href="file:///path"> will only work from a static HTML file loaded from the local filesystem.
For security reasons, a web page is not allowed to access the local filesystem. See Why can't I do <img src="C:/localfile.jpg">?
If you want to speed up access to static files, one option would be to set up a separate light-weight webserver that's dedicated to service your static files (perhaps nginx? or lighttpd? or mongoose?) and use the URL of that server as your MEDIA_URL.
Well, you should try it out. If django is treating the static content path as just another constant, then your network share path should work just well. But, if it isn't, then you can try defining your own constant like: static_on_network_share or something and use that constant while loading content in templates/html.

Link to download image instead of view image (CANT Change Content-Disposition Header)

I have a Django app and all the static content is handled through NGINX. I want to allow users to download a couple of static resources (photos) in their client by clicking on a link in the markup.
To make the link/resource downloadable, I know I could change the Content-Disposition. Unfortunately, since all my static assets are being served through NGINX, this seems problematic. I don't want to create a view just to change the Content-Disposition and make the image downloadable.
Is there anything that I can do here via jQuery or otherwise?

Django where to put static files

I am using Django to create a small web app, however I do not know where i must put my HTML and JS files. I don't want to use the templateing system because I have no need to pass the values from Django directly to the HTML template, Instead the HTML will be static and I will fetch all the data necessary and send the data to be input back into the database using AJAX with Jquery.
My Question is where must I put my HTMl and JS files so they are accessible from the web browser and will be in the same directory so that I can send my ajax requests to something like
http://localhost:2000/webapp/RPC/updateitem/ (more stuff here)
and where the HTML files are
http://localhost:2000/webapp/index.html
Thanks,
RayQuang
You let your main webserver (the one you're running django on) deal with the static files. In most cases this means that you simly server the files through apache (or lighttpd or cherrypy or whatever). Django is only ment for the rendering of dynamic things and thus should not be used for serving static files.
If you're running from a development server (which I can't recommend), this tutorial will help you through setting it up: Serving static files