Why isn't favicon shown up in production? - python

Favicon isn't shown up in the tab though Django's response code is 200. Static files are located in 'myproject/build' (and 'settings.py' in 'myproject/server', and app in 'myproject/blog'). Though HTML page with css and js files is served well. And 'favicon.ico' is located in the same directory as 'index.html' page
I tried to open a page by "localhost:8000' address in different browsers and I got the same thing: favicon isn't shown up in the tab. I also looked at "Sources" in developers options: there aren't any 'favicon.ico' file
settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
urls.py (in 'server/'):
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('blog.api.urls')),
re_path('.*', TemplateView.as_view(template_name='index.html'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
index.html (in 'build/') :
<link rel="shortcut icon" href="/favicon.ico" />
Console:
[10/Jun/2019 09:57:29] "GET /favicon.ico HTTP/1.1" 200 2242
UPD: I ran command python manage.py collectstatic but I still get the same issue with favicon

your favicon located in static folder:
<link rel="shortcut icon" href="{% static 'favicon.ico' %}" />

Related

DJango not finding static files, despite STATIC_URL and STATIC_ROOT set

I have just started learning DJango and am trying to use css file for my html template. As per the documentation, I have added the STATIC_URL and STATIC_ROOT to my settings.py file as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
BASE_DIR is already set as follows:
BASE_DIR = Path(__file__).resolve().parent.parent
In the template, I am using the css file as follows
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
and the style.css is present at location DJango_App/static, DJango_App being my project directory with the manage.py file. Still I am getting error
"GET /static/style.css HTTP/1.1" 404 1653
DEBUG is set to True
Directory structure is:
DJango_App
|->DJango_App
|->(settings.py, urls.py, views.py, etc)
|->templates
|->(html templates)
|->static
|->style.css
How do I resolve this?
Update:
Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS, see:
https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Original:
Looks like you did not add the static url handler to urlpatterns.
Serving static files during development require add to urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

My css changes are not getting reflected when i run my website

In my directory i have de following:
-Proyecto
-proyecto
-static
-css
styles.css
-images
-tienda
in my settings.py file i already added my STATICFILES_DIRS:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
And in my template called "tienda.html" i have the following:
{%load static%}
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
<h3>TIENDA</h3>
when i run my website with my localhost, no css changes are reflected.
I already tried deleting de cache from the explorers but still i canĀ“t see any changes.

Unable to access static files in Django - Development server

I am trying to access static files in browser but unable to access them. My static settings insettings.py are below
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
My urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', FrontPage.as_view()),
# url('', include(router.urls))
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My folder structure
projectname
projectname
settings.py
urls.py
app1_name
static
bs4
css
cover.css
admin
templates
My template.html
<link href="/static/bs4/css/cover.css" rel="stylesheet">
The above link isn't loading the static file. I'm getting 404 on hitting above url. Please let me know where am I going wrong.
All you need is an update of your settings:
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
Use: <link href="{% static 'bootstrap/css/cover.css' %}" rel="stylesheet">
Make sure to use {% load static %} at the top of your template.html and you are all set.

How to make django serve static files in development?

For some reason my django project doesn't see my static files, giving me 404 error, like so:
"GET /static/js/main.js HTTP/1.1" 404 1757
I do everything in accordance with the official guide. Here is my project structrure:
PMpro:
-PMpro (main project)
-users (app)
-tasks (another app)
-static
--js
--css
--etc...
-templates
--template files...
-manage.py
My settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py file:
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', TemplateView.as_view(template_name='index.html'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In my .html file I's collecting static files and linking to corresponding css/js file like so:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/organicfoodicons.css' %}" />
I'm pretty sure I do everything in accordance with the official django guide, and yet my project doesn't see/serve my static files.
I have already went through a number of similar problems people were posting here, and I have everything im my code exactly as it was advised, but the problem is still there.
Folks, any suggestions?

CSS is not working in Django app, no clue what is wrong

I know this is a repeated question, but I am unable to find any answers that would make the static files run for me. I am using Django version: 1.10.5 and python version: 3.4.3...
I have read the official documentation too, no luck in solving my problem...
The following is my project structure:
myproject4
/myapp/
/__pycache__/
/migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
/myproject/
/__pycache__/
__init__.py
settings.py
urls.py
wsgi.py
/static/
/css/
hpage.css
robot.css
/images/
-- Images --
/js/
-- JavaScript Files --
/template/
hello.html
manage.py
Here's what all I have tried:
{% load staticfiles %}
for my hello.html page, right at the top,
<link href="{%static 'css/hpage.css' %}" rel="stylesheet" />
<link href="{%static 'css/robot.css' %}" rel="stylesheet" />
for linking my CSS files in hello.html (it makes use of 2 CSS files).
I have tried {{ STATIC_URL }} way of doing the same and the necessary stuffs I am supposed to do too, but found no luck there.
When I use the <style> </style> tags, the css works perfectly, but that's not what I am looking for.
settings.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
#Other stuffs go here
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/static/',
)
urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from myapp.views import hello,user_profile
from myapp import views
admin.autodiscover()
urlpatterns = [
url(r'^admin/$', admin.site.urls, name='admin'),
url(r'^hello/$', views.hello, name='hello'),
url(r'^hello/user_profile/', views.user_profile, name='user_profile'),
]
views.py:
from django.shortcuts import render, render_to_response
from django.template.context import RequestContext
def hello(request):
return render_to_response('hello.html')
def user_profile(request):
return render_to_response('user_profile.html')
Kindly guide me where I am going wrong...
Thank You in advance :)
EDIT: In my settings.py file, DEBUG = False because I have a file called 404.html that gives out the error page for me as default.
These are the steps I have followed to include CSS to my Django Project:
First, I have added in the settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
Second in the urls.py (app folder)"
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Copied all CSS files in the Project folder (same level with manage.py)
Include/Declare CSS in a template (I included mine in base.html)
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'assets/css/bootstrap-theme.css' %}" media="screen" >
<link rel="stylesheet" href="{% static 'assets/css/main.css' %}">
<link rel="stylesheet" href="{% static 'assets/css/style.css' %}">
</head>
Run:
python manage.py collectstatic
and reload/restart the browser or delete cache.
Make changes to urls.py like bellow. and look at documentation for your ref.
from django.conf.urls.static import static
urlpatterns = [
rl(r'^admin/$', admin.site.urls, name='admin'),
url(r'^hello/$', views.hello, name='hello'),
url(r'^hello/user_profile/', views.user_profile, name='user_profile'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Categories