Flask does not loads css with url_for() - flask

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')}}">

Related

Django ckeditor codesnippet plugin shows codes without color

I am using ckeditor in my django project, everything is working fine, but when add code snippet there is only simple text code without colors but in my admin panel it is normal.
add it in html
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/default.min.css"> 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/monokai.min.css"> 

CSS not showing on Github pages

My CSS is not showing. This is my first time using Github Pages and I'm really confused.
The link to the site is: https://jevoncochran.github.io/index.html
The link to the repo is: https://github.com/jevoncochran/jevoncochran.github.io
I tried playing around with the CSS link in the html file
this is what I have now:
<link href="css/index.css" rel="stylesheet">
In your index.html, the above line should be:
<link href="index.css" rel="stylesheet">

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'),)

Get the absolute static file URL in my Django template

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.

Django template not loading javascript and css properly due to urlpatterns

When this one runs everything goes fine:
(r"^newobject$", "views.myobjects.newobject"),
All the CSS + JS files are properly fetched from:
static/css/...
static/js/...
When this one runs:
(r"^mybjects/(([a-z]|[A-Z]|[0-9])+)$","views.myobjects.loadobject"),
All the css and JS files that are being fetched, are run trough the urlpatterns and are returning my defailt page:
(r"", 'views.main.index'),
This makes all my CSS and JS code to actualy be HTML. My guess is that i'm giving some noob mistake. Is there any common reason why this should happen? And how to fix it?
Edit:
Css example:
<link href="static/css/style.css" type="text/css" rel="stylesheet">
JS example:
<script src="static/js/libs/date.js" type="text/javascript"></script>
See the difference:
when you access *some url*/newobject the static/css/style.css refers *some url*/static/css/style.css*
when you access *some url*/newobject/whatever the static/css/style.css refers *some url*/newobject/static/css/style.css*
If your URL will always be floating around in depth, include your javascript and CSS using URLs relative to the server root (start them by /) instead of relative to the current dir.