Get the absolute static file URL in my Django template - django

I want to know how can I get the absolute URL of my static file directly in my template in Django ?
For now in my template :
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
return
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
How can I get on dev:
<link rel="stylesheet" href="http://127.0.0.1:8000/static/css/bootstrap.min.css">
on production
<link rel="stylesheet" href="https://mycompany.com/static/css/bootstrap.min.css">

There are two options:
recommended: use the sites framework to render the appropriate domain
not recommended: store your current domain as a Django setting in the settings file you use depending on your environment
I usually go for (1), the only downside being that you have to update the current domain in the DB, but that usually happens just once per deployment.
Then the appropriate domain will be displayed irrelevant of where code is running; you should always use the static tag in your template, rather than handling the display of the domain manually.

Related

Flask does not loads css with url_for()

I am writing a flask web application that needs to have css. I have used the correct syntax, i think.
my web application is at its start and the css is simple too. I don't know why the flask won't load my css.
the code i used to link css to the html file
<link rel="stylesheet" href="{{url_for('static', filename='style.css')}}">
image contains file structure, code and localhost browser image
You are getting HTTP 404 errors trying to get /static/static/style.css. You have the line correctly in the question here, but in the Jinja template (on the screenshot), it reads:
<link rel="stylesheet" href="{{url_for('static', filename='/static/style.css')}}">
i.e. there's /static/ added in the filename. Remove it and make it read
<link rel="stylesheet" href="{{url_for('static', filename='style.css')}}">

error with an url that does not exist in my project in django

I was having this issue just in my production environment. I have an icon that is in my static files. I can load it through https://mydomain/static/img/favico.ico, but at the same time I get a 500 error for the following url https://mydomain/favico.ico . <- this url does not exist in my project.
I can't replicate in my local environment. I already did collectstatic.
The default favicon URL is /favicon.ico.
You should change it in your template to what you need:
<link rel="shortcut icon" type="image/png" href="{% static 'img/favico.png' %}"/>
make change in href and type according to your favicon file.

Resource interpreted as Stylesheet but transferred with MIME type application/x-css:

I am trying to load css file in HTML template in python Django and getting this error
Resource interpreted as Stylesheet but transferred with MIME type application/x-css:
I am using Pydev , Eclipse, Django framework.
You need to give STATIC URL AND STATIC_ROOT values in settings file of django project and then create static folder inside your App folder. Place all your css and js files inside this folder.Now add this tag in your template {% load static %} and give location of css like <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" />
Example
STATIC_URL='/static/'
STATIC_ROOT=os.path.join(BASE_DIR,"static")'
STATICFILES_DIRS=(os.path.join(BASE_DIR,'Get_Report','static'),)

Flask.url_for() error: Attempted to generate a URL without the application context being pushed

I have a trivial app where I'm trying to redirect the favicon per:
http://flask.pocoo.org/docs/0.10/patterns/favicon/
app = flask.Flask(__name__)
app.add_url_rule('/favicon.ico', redirect_to=flask.url_for('static', filename='favicon.ico'))
But this fails with:
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
So, guessing, I try this:
app = flask.Flask(__name__)
with app.app_context():
flask.current_app.add_url_rule('/favicon.ico', redirect_to=flask.url_for('static', filename='favicon.ico'))
But get a different error:
RuntimeError: Application was not able to create a URL adapter for request independent URL generation. You might be able to fix this by setting the SERVER_NAME config variable.
What is going on?
According to the doc:
Setting a SERVER_NAME also by default enables URL generation without a request context but with an application context.
since you're using app_context, you may set the SERVER_NAME Configuration Value.
By the way, as the doc:Adding a favicon says:
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
the above line should be enough for most browsers, we don't have to do any other things.
Late answer, I've just run into the same problem. I don't see a big downside in handling the redirect like this instead:
#app.route('/favicon.ico')
def favicon():
return redirect(url_for('static', filename='favicon.ico'))
This prevents url_for from being called before the application is ready.
To give a counterpoint to using a link in the HTML only, it's a good practice for every site to have a favicon.ico and robots.txt at the root level - even if they're empty. It avoids problems like this and other unnecessary errors that adds noise to logs.
Don't put this in the app but in the html file
<html lang="en">
<head>
<title>{{ title }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width/2, initial-scale=1">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>

How can I change the Django Admin Static Files url?

I'm hosting my Django project on my own server, and exactly as the docs say, the Django admin media stop showing up.
The solution is simply to host it yourself, which I'm doing. The problem I'm having is that the url the Django admin is using to try to find them is incorrect. Specifically, Django is looking at
<link rel="stylesheet" type="text/css" href="/ceasarb-cfa/admin/css/base.css">
when I want it to look at
<link rel="stylesheet" type="text/css" href="/ceasarb-cfa/static/admin/css/base.css">
My question is, how can I change that path?
Intuitively, I've tried adjust the ADMIN_MEDIA_PREFIX file in settings.py (currently set to /ceasarb-cfa/static/admin) but fiddling with that value didn't seem to change anything.
My guess is that your settomgs.py you have:
STATIC_URL = "/ceasarb-cfa/"
but it should be
STATIC_URL = "/ceasarb-cfa/static/".
Here's more documentation on that setting:
https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATIC_URL