I am trying to make a simple website using Python and Flask. Right now, I have the HTML and CSS finished, but my page does not show any of the CSS. My files are in the following hierarchy:
/static
/static/css/style.css
/static/js/skel.js
/templates
/templates/index.html
I have the following code in my html file:
<script src="../static/js/jquery.min.js"></script>
<script src="../static/js/config.js"></script>
<script src="../static/js/skel.min.js"></script>
<script src="../static/js/skel-panels.min.js"></script>
<noscript>
<link rel="stylesheet" href="../static/css/skel-noscript.css" />
<link rel="stylesheet" href="../static/style.css" />
<link rel="stylesheet" href="../static/css/style-desktop.css" />
</noscript>
When I run this, the terminal displays "304" for the JS files and gets them from "/static/js/skel.js" which I think is correct, but it then displays "404" for the CSS files and gets them from "/css/style.css". I am not quite sure why this is happening - shouldn't this be retrieving files from /static/css/style.css? How can I fix it?
Thanks for the help.
Your on-disk layout is not the same thing as the routes served by your Flask site. Do not rely on the relative paths between templates and static.
Instead, have Flask calculate the routes to static files for you, using the url_for() function in your Jinja template:
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/config.js') }}"></script>
<script src="{{ url_for('static', filename='js/skel.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/skel-panels.min.js') }}"></script>
<noscript>
<link rel="stylesheet" href="{{ url_for('static', filename='css/skel-noscript.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='css/style-desktop.css') }}" />
</noscript>
If the template is really what you have, and your layout is really what you list, then they don't match:
/static/css/style.css
does not match
<link rel="stylesheet" href="../static/style.css" />
Instead you should have
<link rel="stylesheet" href="../static/css/style.css" />
or move style.css up one folder. Note however that IIRC you can't have ../ in path, because static folder is supposed to be at same level as your flask app script. So you should be able to just have
<link rel="stylesheet" href="static/css/style.css" />
but as others have mentioned it is better to have flask generate the url for you via url_for (see example in step 6 of tutorial).
I had the same problem. This solved it:
open your config.js and add static/ in front of the css/... in all links like href="css/style.css" that they look like href="static/css/style.css" and it should be fine.
The question is already 5 months old but I hope I can help someone who has the same problem.
Related
This is a weird problem. On my login page the CSS will always load correctly.
But once i sign in to my application and get redirected to the dashboard, the CSS will almost always fail to load the first time although when i press F12 to see in the network tab if the CSS files are loaded; they are.. After some inactivity when i refresh the dashboard again the CSS may fail to load although the network tab sais it did load properly..
All CSS files exists and the HTML looks like this in the view:
{{-- CSS --}}
<link rel="stylesheet" href="{{ asset('css/bootstrap.css') }}">
<link rel="stylesheet" href="{{ asset('css/font-awesome/fontawesome.css') }}">
<link rel="stylesheet" href="{{ asset('css/font-awesome/solid.css') }}">
<link rel="stylesheet" href="{{ asset('css/font-awesome/regular.css') }}">
<link rel="stylesheet" href="{{ asset('css/choices.css') }}">
<link rel="stylesheet" href="{{ asset('css/common.css') }}">
<link rel="stylesheet" href="{{ asset('css/dashboard.css') }}">
#stack('css')
I have also tried to clean the browser cache with no luck.
Also i tried to composer dump-autoload, i don't know if that would help in any way but i tried it.. What could be causing this?
I have 2 images for reference.
The first one is when this weird error happens and the other one is how it looks just after i refresh it.
I would like to know, why on fresh Laravel install, the following code does not link Bootstrap CSS file with the blade.php file, this is what I believe the correct code to make it work:
<link href="{{ asset('public/css/app.css') }} rel="stylesheet"/>
The only way I made it work was this way:
<link rel="stylesheet" type="text/css" href="/public/css/app.css">
However I want to do it with asset, so I would like to know what is the proper way.
You can use one of the following options:
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" >
OR
<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet" type="text/css" >
NOTE: This will work if your directory structure is like this: /public/css/app.css
It looks like you are trying to access the bootstrap CSS file from the public folder but by default, there is not one until you have placed it.
Also when you are using the app.css file then everything is working because it is a compiled CSS file which is a combination of bootstrap, font awesome and other CSS files. Laravel kept these files if you are using VueJs with laravel.
you need to perform:
npm install bootstrap
wait and then perform:
npm run dev
it will compile sass with all dependency file into app.css and app.js
and then you need to include it into any blade.php file
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
<script src="{{ asset('js/app.js') }}"></script>
I use Hugo to generate website. I have a syntax highlight css (e.g. tomorrow-night-blue.css) in /themes/hyde/static/css, a highlight.pack.js in /themes/hyde/js and have the following code in the header.html
<!-- CSS -->
<link rel="stylesheet" href="{{ .Site.BaseURL }}css/print.css" media="print">
<link rel="stylesheet" href="{{ .Site.BaseURL }}css/poole.css">
<link rel="stylesheet" href="{{ .Site.BaseURL }}css/syntax.css">
<link rel="stylesheet" href="{{ .Site.BaseURL }}css/hyde.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400italic,700|Abril+Fatface">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" href="{{ .Site.BaseURL }}css/tomorrow-night-blue.css">
<script src="{{ .Site.BaseURL }}js/highlight.pack.js"></script>
<script>
hljs.initHighlightingOnLoad();
</script>
The code chuck on the rendered website only has a blue background colour but the texts are not highlighted as shown below.
I think the syntax highlight css conflicts with other css, but I don't know where the conflict is and how to resolve it.
Source files are hosted here, if that helps to debug.
Did some search. Couldn't find a solution for my particular issue, but still managed to highlight the code following this post here.
https://github.com/yihui/hugo-xmin/pull/5/files
Essentially, one needs to put something like following in the header.html
<link href="//cdn.bootcss.com/highlight.js/9.12.0/styles/github.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>
<script src="//cdn.bootcss.com/highlight.js/9.12.0/languages/r.min.js"></script>
<script>
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
</script>
I've put both the link to css and script in the header.html as opposed to putting them separately in hearder.html and footer.html as shown the link. It works either way.
I'm still open to more specific solutions.
The third link in the below code is supposed to be my own stylesheet for a django app I'm creating. There is only one style sheet in the entire application, but for some reason when I went to add new styles today none of them were taking place.
I deleted all the CSS from the file to see if it impacted the site, and it had no effect. Then I deleted the file all together, and still no change in appearance.
Then I commented out the <link /> to the stylesheet and the website loses its styles! I assume one of two things is happening
I'm editing the wrong stylesheet
Something is messed up with the browser. However, as I mentioned, this is the only stylesheet in the entire app directory, so I don't know how there could be another style sheet that the link is referring to. Any help is greatly appreciated!
<head>
{% load static %}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.6.3/font-awesome.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"/>
<link type = "text/css" rel = "stylesheet" href= "{% static 'polls/stylesheet.css' %}" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="{% static 'polls/script.js' %}"></script>
</head>
Following is my code in an HTML page:
<html>
<head>
<link rel="manifest" href="{{ STATIC_URL }}manifest.json" />
<link rel="stylesheet" href="{{ STATIC_URL }}ng-grid.min.css" />
</head>
</html>
Both manifest.json and ng-grid.min.css are served from the same static folder.
ng-grid.min.css was loaded as soon as the page was rendered but manifest.json file is not loaded.
On inspecting using developer tools I couldn't find a request for manifest.json.
Am I missing something?
You missed a '/' in the statement.
<link rel="manifest" href="{{ STATIC_URL }}/manifest.json">