📜  favicon django - Python (1)

📅  最后修改于: 2023-12-03 15:15:02.684000             🧑  作者: Mango

Favicon Django - Python

Favicon is a small icon displayed in the web browser's address bar, tab bar, and bookmarks bar to denote a website. Django is a high-level Python web framework that follows the model-view-controller (MVC) architectural pattern.

In this article, we will discuss how to add a favicon to a Django web application.

Step 1: Create Favicon

Create a favicon file in .ico or .png format. It's recommended to use a square image with a size of 16x16 or 32x32 pixels.

Step 2: Add Favicon to Django App

Place the favicon file in the static directory of your Django app. If you don't have a static directory, create one.

# settings.py

...

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / "static",
]

...
<!-- base.html -->

{% load static %}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{% block title %}Favicon Django{% endblock %}</title>
    <link rel="shortcut icon" href="{% static 'favicon.ico' %}" type="image/x-icon">
    <link rel="icon" href="{% static 'favicon.ico' %}" type="image/x-icon">
</head>
<body>

{% block content %}
{% endblock %}

</body>
</html>

The above code adds two links to the head section of your HTML file. One for the address bar and the other for the tab bar. The {% static %} tag is used to reference the favicon file in the static directory.

Step 3: Run Django App

Run the Django app to see the favicon in action.

python manage.py runserver
Conclusion

That's it! In this tutorial, we learned how to add a favicon to a Django web application. Now your website will have a professional look and feel with an identifiable icon.