I get the templates files, I divide them to static files and templates files.
As you know I can not correct open the html in browser now, because thier path all changed.
I can in the templates files to change every link or script and image path one by one, but its a trouble things, takes our away many time.
Is there a simple way to do that?
EDIT
I changed to this, but not works for me:
{% load static %}
<!--<link rel="stylesheet" href="../static/css/default.min.css?t=227" />-->
<link rel="stylesheet" href="{% static 'css/default.min.css?t=227' %}" />
in the browser debugger, it become this:
<link rel="stylesheet" href="/static/css/default.min.css%3Ft%3D227">
If I understand the question correctly, you need to use the {% static %} tag in the templates, like this:
{% load static %}
...
<img src="{% static 'images/favicon.png' %}">
For more information, check my detailed explanation.
Related
I know this is a duplicate question, but I was trying to add a favicon.ico file to my site on localhost:8000 made with Django. The favicon exists on templates\articles (in an app called articles), and I've tried everything on stackoverflow, youtube, and used realfavicongenerator.net, but nothing works. Do I have to define the Django URL/view for the ICO file, as localhost:8000/favicon.ico brings up error? Here's my (simplified) code by the way:
<title>Newsreed | Articles</title>
<link rel="shortcut icon" type = 'image/x-icon' href="favicon.ico">
What should I do, because I've been struggling with it for several days now and tere has been no solution on anything.
I like to use:
https://www.favicon-generator.org/
They let you upload an image to use and it will output a directory with all scaled versions of the image for all manner of devices your page could be viewed on including the html that you will need to copy and paste.
Fairly sure the syntax for the href should be: href="{% static 'imgs/favicon.ico' %}
Best way to have a favicon on every page on your site is to link it to the base.html with href="{% static 'articles/favicon.ico' %}. Mind that you need to put {% load static %} in the template too.
Code standard favicon links at the top of your template or base.html and make sure the icons are there in the directory/s :
{% load staticfiles %}
<head>
<link rel="shortcut icon" href="{% static "img/favicon.ico" %}">
<link rel="icon" href="{% static "img/animated_favicon1.gif" %}">
You can use favicon-generator.org to create new favicons.
Im new to using Flask/Jinja but I think that I have my syntax correct yet my css file will not link/modify my app page.
my css file sits in the path:
program/src/static/css/main.css
In my base.html file I tried to link the main.css file using the following:
<link rel="stylesheet" href="{{ url_for('static, filename='css/main.css') }}"/>
I should note that Im utilizing bootstrap css (which is working) which is linked above the link to my file in the base.html.
Currently trying to use the css file to alter a linked page written using the jinja format
{% extends "base.html" %}
{% block content %}
{% endblock %}
yet when I run the page none of my CSS code that I write links to the app page when it runs.
thanks in advance!
If the snippet is your actual code, then you might want to close that string,
<link rel="stylesheet" href="{{ url_for('static, filename='css/main.css') }}"/>
^
I'm trying to learn Django at the moment and am trying to make sure I'm not doing anything stupid.
I'm in the process of making my web page more modular in the sense that I am removing hardcoded values in the template (base .html). Doing so, I'm trying to convert hardcoded CDN references (jquery, bootstrap, etc.) to modular pieces that can be included in every web page. Doing so will allow me to change a single file in the future, instead of being forced to go to every web page and make that change.
However, I'm slightly confused. I'm trying to determine if it would make sense to copy them into a html file and use Django's {% include '' %} template tag to directly include the cdn portions, or if using Django's static include would be more appropriate.
So what exactly is the best route? It seems like it would be very easy to use template includes for everything static in all honesty. Why not use it to include javascript or css?
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.
We call them 'static' simply because they aren't dynamic i.e the contents of these files are relatively fixed, either by design or by it's intrinsic characteristics (eg: binary content like images) and thus does not need to processed by our application server.
We differentiate them from other files because it's advisable to serve these static files at a lower level, for example, using nginx. This allows us to serve these files faster as is which leads to performance gains. It also allows easy caching.
But when using a CDN, you offload this work from your server to somebody else's server.
Now coming back to your question. You shouldn't have to declare your resources in every template. Usually, base.html contains the base of the page which can then me extended (read: template inheritance) by more specific (children) templates.
To understand this quickly, here's an example:
base.html:
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="~~CDN HERE~~">
<script src="~~CDN HERE~~"></script>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
article.html
{% extends "app/base.html" %}
{% load static %}
{% block body %}
<h1>{{ page_title }}</h1>
<img src="{% static 'app/img/detective.png' %}" alt="detective" />
{{ page_content | safe }}
{% endblock %}
Now for every article on your site, you render the article template which automatically extends the base removing the need to mention your css/js files for multiple pages.
If you're using different resource files for different pages, you can creation an additional block like {% block css %}{% endblock %}
and then add this to your article.html
{% block css %}
<link rel="stylesheet" href="{% static 'app/css/article.css' %}">
{% endblock %}
Notice how I'm using static for image, which is served directly by nginx.
Theoretically, you can club your CDN links into a file and then include it in base.html, but it just leads over modularity which causes redundant complexity.
Let me know if you have any issues!
I am making a website using html, css, flask and jinja2.
I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in.
How would I link a stylesheet to a jinja2 template. I have looked around on the internet but cannot find out how.
Here is the css stylesheet link; should I change this, or the python code?
<link rel="stylesheet" type="text/css" href="styles.css">
here is my flask code:
#app.route('/')
def resultstemplate():
return render_template('questions.html', head='Welcome!')
here are the locations of the files:
/python-code.py
/templates/template.html
/templates/styles.css
All public files (the ones that are not processed, like templates or python files) should be placed into dedicated static folders. By default, Jinja2 has one static folder called static.
This should fix your problem:
Move /templates/styles.css to /static/styles.css
Update your code with following code, that will be translated into correct file location:
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
More info on static files in Jinja2 is here.
<link rel="stylesheet" type="text/css" href="styles.css">
href value must be within quotes.
make sure the file name and path are proper
OR try the below
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}"/>
The order of handler might cause the problems:
url: /stylesheets static_dir: stylesheets
url: /.* script: helloworld.application
will work instead of
url: /.* script: helloworld.application
url: /stylesheets static_dir: stylesheets
you should use the super() block style .
see the code below:
{% block style %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}"/>
{% endblock %}
Tried almost every solution on Stack Overflow. It only worked for me when I placed the static folder in the same directory as my run.py file.
I changed my folder structure from:
app/
views
static
templates
run.py
To:
app/
views
templates
static
run.py
I guess moving the run.py instead would work too. Have a look at this Jinja Templating Tutorial for extra info. Not sure why I had to change the structure for it to work though.
To add to what's been said here, be sure to update Jinja2 to v2.10, as lesser versions seem to cause this same bug. Cheers!
I've searched for answer for this question, but i havent found any solution for my problem. I want to link css to my project, but just cant handle how STATIC_URL work
<head>
{% load static from staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">
</head>
it's to much code to post here so here are the links:
settings.py: http://pastebin.com/9Bsg3u1h
And I render with context_instance=RequestContext(request) parameter of course.
I got files structure like this:
Django_project
...
appname
templates
static
I tried also to place static directory in many project, in appname, and even in templates.
Can someone explain me how should it look for my project?
Use the builtin static, not static from staticfiles:
<head>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">
</head>
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#static
Django ships with a static template tag. You can use this regardless if you’re using RequestContext or not.
Note
The staticfiles contrib app also ships with a static template tag which uses staticfiles' STATICFILES_STORAGE to build the URL of the given path. Use that instead if you have an advanced use case such as using a cloud service to serve static files...