How to handle favicon request in Django rest framework? - python

I am coming from Node.js and I have a nextjs frontend that communicates with a Django REST API. However, each time I make a request to my Django API, the app makes an additional request:
GET /favicon.ico
I've tried to add favicon route in urls.py
favicon_view = RedirectView.as_view(url='/static/images/favicon.ico', permanent=True)
url(r'^favicon$',favicon_view)
But it doesn't works.

Problem is in the regex
...
url(r'^favicon\.ico$', favicon_view),
...
As per regex $ indicates the end of the string so whole URL needs to put before this

you dont a view for favicon if you setup your static root in settings.py you go to your frontend and use load static tag
{% load static %}
and in favicon link you link it
<link rel="icon" href="{% static 'images/favicon.png' %}" sizes="16x16" type="image/png">
after that you run python manage.py collectstatic

Related

CSS doesnt link to HTML in django

I am watching beginner course on django . So basically i am running server from cmd and when i want to change anything on css file it doesnt change live . My css file is in static folder in root directory.
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
my html :
{% load static %}
<link type = "text/css" href="{% static 'style.css' %}" rel="stylesheet">
<h1>Hi welcome to my page </h1>
You have to reload the page and the server once you have made the changes. Stop the server after the changes and start it again and reload the page.
A quick way exist, the shortcut to reload cache:
Ctrl + Shift + R
Then just reload page

Static folder problems on multiple apps project on django

I'm new on Django and I have some problems with the static folder.
I created a "static folder" on the project root and inside I put folders for each app.
Django admin page is charging correctly
But CSS, JS, images and others on my index isn't.
How can I avoid have this problems in the future? For your answers, thank you so much.
settings.py
I set the {% load static %} in my html document
and i write the static tag like this example:
<link rel="stylesheet" href="{% static 'principal/css/bootstrap.min.css' %}" type="text/css">
Yo haven't created STATIC_DIRS where the django will find CSS,javascript files and the create the STATIC_ROOT where the django will store all the files
then run python manage.py collectstatic to let setup all the files

bootstrap_find_resource do not serve local files

I am using Flask-Bootstrap package for my Flask application, however when I am creating a template as described in docs:
{% extends "bootstrap/base.html %}
<!-- Rest of the template is here -->
page source displays this:
<!-- Bootstrap -->
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
I don't know why this resource is taken from cdnjs.cloudflare.com instead of local files in Flask-Bootstrap package.
cdnjs.cloudflare.com is referred in more places in page source code.
Can I somehow change this behavior in my template, to make it serve resources from local directory?
base.html template uses bootstrap_find_resource template filter, so I guess it have something to do with CDN settings, how can I change them?
Add this to your configuration :
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
Check out Flask-Bootstrap link for more info.

How do you add a favicon to HTML page?

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.

Flask Templates couldn't load css

I followed this tutorial for develop templates with flask http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates
My file tree is the next one:
/static
/js
bootstrap.min.js
jquery_1.11.3.js
/css
bootstrap.min.css
/images
/templates
index.html
/python_venv
server.py
server.py code:
#app.route('/subdomain/')
def getPrevisionPoblacion():
return render_template('index.html')
And css link inside index.html code is the following:
<script src="/static/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/static/css/bootstrap.min.css"/>
<script src="/static/js/jquery_1.11.3.js"></script>
nginx config:
location /subdomain/{
root /apps/subdomain/static;
uwsgi_pass unix:///tmp/subdomain.sock;
include uwsgi_params;
}
When I check it on Chrome, the index didn't load the CSS files. I checked the network with Developer's tools and the error is 404.
I tried also similar code that i saw on this unresolved question without success
Inline CSS background:url() not working in Jinja2 template
Any help about this ?
The problem was the server.
I had to serve static folder for flask template can load css.
I wrote in nginx config file the next:
location /subdomain/static)/ {
root /opt/apps/content_folder/static;
}
I don't use url_for() function y wrote the resource's URL as:
static/css/my_css_template.css
Finally check out #app.route() inside the file that render the template was correct then flask template could access to css, images and js.
if you put the static files directly under the /static folder, this should work
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}">
if you do not want that, follow the instructions in this questions approved answer,
Link to Flask static files with url_for

Categories

Resources