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...
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.
To practise my Django skills, I want to try to host a game on my website. The game was made in Unity and packaged using WebGL.
I used the index.html created by WebGL as a Django template and created the URL path and view in the relevant locations. WebGL creates two folders, Build and TemplateData that are referenced in index.html.
I have tried putting these folders in with the html templates, and on every level in my Django folder structure.
The outcome is the same wherever I put the folders. The GET command is sent but a "Not Found: /TemplateData/filename...." is shown for a few files
Does anyone know how to see where the GET command is looking, or just where the folders should go?
Thanks,
Sophie
After some more messing about I found that you can just wham everything into the static folder and then reference it in index.html the same way you pick up css files in base.html
{% load static %}
.....some more code
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "{% static
'Build/Juno_WebGL.json' %}", {onProgress: UnityProgress});
</script>
Do this for every reference to the files in index.html
I ran into this same issue recently and came across this thread. Here was my solution for anyone who may come across this in the future.
take the index.html file generated and put it in your templates folder, create a link through to this template using the standard django, url, view, template flow e.g:
urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('unity/', views.unity, name='unity'),
]
views.py:
def unity(request):
return render(request, template_name='mainapp/unity.html')
Take your WebGL build files and put them somewhere in your static folder. Finally edit your index.html so that it includes {% load static %} and look for any href's in the file and point these to your static folder, do the same for any scripts. E.g. My Unity builds WebGL application had a 'var buildUrl = "Build";' line which I had to point towards my static folder.
Example:
<!--block content etc.. -->
{% load static %}
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unity WebGL Player | UnityApp</title>
<link rel="shortcut icon" href="{% static 'unity/TemplateData/favicon.ico' %}">
<link rel="stylesheet" href="{% static 'unity/TemplateData/style.css' %}">
</head>
<!-- etc.. -->
<body>
<script>
var buildUrl = "../static/unity/Build";
var loaderUrl = buildUrl + "/Unity App.loader.js";
// etc..
</script>
</body>
</html>
Ok I am new to Python, I am using Django here.
I cannot get my Python to see the CSS file and use it. I have tried hardcoding the path, server restarts, nothing works. I can get it to read a Bootstrap CDN however, I am not sure what is wrong.
My File structure is like so:
-migrations
-static
--music
---images
---style.css (the file that i'm trying to get)
-templates
--music
---index.html (where my link attribute is)
I am trying to load the static CSS file in index.html like so:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href= "{% static 'music/style.css' %}"/>
Here is the CSS:
body {
background-color: red;
}
Thanks in advance!
Add following code in setting.py file
STATIC_ROOT = os.path.join(BASE_DIR,"deploy_to_server")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
And use in your template
<link rel="stylesheet" type="text/css" href= "{% static 'music/style.css' %}"/>
Create static folder in your project
==> static ==> music ==> style.css
==> manage.py
Hope this helps you
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.
I have tried linking a stylesheet to my template in django and it doesnt do anything:
the template(called base.html)
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}base.css">
</head>
<body>
<h1>Welcome to {{ page }}</h1>
</body>
</html>
This is the settings about the static files:
STATIC_URL = '/static/'
STATICFILES_DIRS = ('C:/Users/GAL/PycharmProjects/sqvatPreAlpha/static/',)
The way my project is built:
http://i.imgur.com/o44QSEk.png
What should I do to make it work?
You haven't mentioned if this is a dev or production box. If the latter, make sure you run:
python manage.py collectstatic
This will collect all your static files that you have in the Django project root, and copy them to the STATIC_URL directory. If you are still running into problems after trying this, you most likely have something incorrectly defined in your settings file.
You have to add {% load staticfiles %} at the beginning of base.html
Also, it seems you need to add .cssto your css file.