favicon icons not found , django website with bootstrap at front - django

My website favicon icons are no longer found. It is a simple Django website with bootstrap. It was working a few minutes ago, I tried changing the whole boot strap code.
The log i am getting when the django server is running and I use the users profile:
Not Found: /favicon.ico
[06/Feb/2021 08:53:13] "GET /favicon.ico HTTP/1.1" 404 4502
Session data corrupted
This is were the bootstrap static is coming though in the page:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css"
href="{% static 'blog/main.css' %}">

Most browsers look for the existence of an file called favicon.ico at the root path of your website domain, this controls the icon for the website you can see in your bookmarks folder or the address bar of your browser.
If you don't have one, then it's valid that it would return a Not Found error.
When you deploy to something like Apache, you will have to alias your favicon in a config file. However, while running Django in development mode, the following works
urls.py
from django.views.generic import RedirectView
from django.urls import path
urlpatterns=[
...
path('favicon.ico',RedirectView.as_view(url='/static/images/favicon.ico')),
]

Related

Why django collectstatic is not finding my css

I am struggling with django static files. I want to link "screen.css" stored in "all_statics/css" folder to "index.html".
Here is my html file :
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Gwendolyn:wght#700&display=swap" rel="stylesheet">
<link href="{% static '/css/screen.css' %}" rel="stylesheet" >
</head>
<body>
<h1>Test</h1>
</body>
</html>
In my settings.py, I have set these variables :
STATIC_URL = 'static/'
STATICFILES_DIR = [
BASE_DIR / 'all_statics',
]
STATIC_ROOT = BASE_DIR / 'static'
Here is my project structure :
Project structure
When I run python manage.py collectstatic , the static files from admin app is copied to static folder, but not the files from 'all_statics' .
When I run python manage.py runserver and browse to the index page, I see that the css can't be found (404) :
[18/Jan/2022 11:30:26] "GET / HTTP/1.1" 200 308
[18/Jan/2022 11:30:27] "GET /static/css/screen.css HTTP/1.1" 404 1801
When I run python .\manage.py findstatic css/screen.css I get : No matching file found for 'css/screen.css'.
I have created a sample project to show my problem. You can clone it from https://github.com/doolieSoft/django_project.git .
Do you have an idea why my css can't be found ?
Thanks for your help !

CSS file loading but not working in Django project

I am new to Django, I am working in python 3.5, I linked CSS using Jinja but I am not able to see the CSS style within my HTML file, even thought I am getting 200 status in my network and the CSS file is loaded but the style is not working. Can you help me.
here is my html file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>home</title>
<link rel="stylesheet" type="text/css" href="{%static
'posts/customstyle.css'%}">
</head>
<body>
<div class="container">
<h1>This is the HTML file</h1>
</div>
</body>
and my CSS file is also within 'static' folder within my project folder. I have tried changing the name but nothing is happening.
All I get in the console is:
Resource interpreted as Stylesheet but transferred with MIME type application/x-CSS.
While my network is pretty healthy and I am getting my files uploaded properly.
Here is what I am getting in console:
file uploads sucessfully
style showing but not working

Django 1.8 and the ever confusing "Static Files"

I've fired up a new Django 1.8 project and am hitting the wall of trying to serve static files in the development environment. I'm pretty sure I've followed the docs explicitly, but it's just not working.
Settings.py
STATICFILES_DIR = (
os.path.join(BASE_DIR, 'static/'),
BASE_DIR
)
STATIC_URL = '/static/'
STATIC_ROOT = '/srv/www/cpm2/static/'
urls.py:
from django.conf.urls import include, patterns, url
from django.conf import settings
from django.conf.urls.static import staticĀ·
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^test/', TemplateView.as_view(template_name="basics/base.html")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
templates/basics/base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% load staticfiles %}
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
When I go to http://my_server_ip:8000/test/ none of the CSS is loaded. Viewing the page source, I see this where that CSS link is <link href="/static/css/bootstrap.css" rel="stylesheet"> which is good, but when I try to follow that link directly, it fails giving me a 404 error.
Now, I've set up my Apache server to serve the files directly, they work. So, going to http://my_server_ip/static/css/bootstrap.css works - so I know the files are readable. It's just they don't work in development.
What am I doing wrong? As so many others before me, I'm pulling my hair out!
EDIT Trying to access the file directly gives me this:
Page not found (404)
Request Method: GET
Request URL: http://my_server_ip:8000/static/css/bootstrap.css
Raised by: django.views.static.serve
'css/bootstrap.css' could not be found
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
It doesn't even show a list of places its tried, just that the file isn't found.
You are missing an 's' from STATICFILES_DIRS.
Also, you shouldn't be including your BASE_DIR in that setting - that could end up serving all your python code, which would be a bad idea.

Django static files on Heroku looping into HTTP redirect in a Safari browser

I thought I had properly done my Django to Heroku deployment, but I'm stuck in an infinite loop of HTTP redirect and I can't figure out the way out.
Here is my settings.py
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, '../static/'),
)
I have my stacic files stored in project_root/static.
When I ask for my CSS file through a HTTP request, I have get it right (http://heroku-app-name/static/styles/base.css will render my CSS).
But when I request my main page, I have no CSS in my Safari browser ... but everything is right in a Firefox browser.
In my index page code (in browser):
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="/static/styles/base.css" media="all" />
In my Safari's developer box I see some strange redirections:
http://myapp.herokuapp.com/static/styles/contracts/contracts/contracts/
[Error] Failed to load resource: too many HTTP redirections (contracts, line 0)
Where do you think my Djando is flawed?

css pulling from the wrong location with {{ STATIC_URL }} on my form page only

I'm using django 1.3
I'm using a base.html template which my pages inherit from. Everything works fine except for one page (which has a form on it).
That page is trying to call the CSS from /links/addlink/css/site.css but this is the wrong location. It is in /static/css/site.css
In my base.html I have <link rel="stylesheet" href="{{ STATIC_URL }}css/site.css" type="text/css" charset="utf-8" />
If I create a completely seperate template and use the full path like <link rel="stylesheet" href="http://127.0.0.1:8000/static/css/site.css" type="text/css" charset="utf-8" /> then my page renders correctly.
What is wrong?
You are not using RequestContextin your view.
See here: Referring to static files in templates