How to configure django static files for development?

Create a directory for your static files

In order to configure Django static files for development, the first step is to create a directory for your static files. This directory should be located in the same directory as your Django project. To create the directory, open your terminal and run the following command:

mkdir static
This will create a directory called “static” in your current directory.

Add the following code to your settings.py file:

In order to configure Django static files for development, you need to add the following code to your settings.py file:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

This code will tell Django where to look for static files. The STATIC_URL variable defines the URL prefix for static files, while the STATICFILES_DIRS variable defines the directory where the static files are located. In this example, the static files are located in the static directory inside the Django app.

Once you have added the code to your settings.py file, you can move on to the next step in configuring Django static files for development.

Create a “static” directory inside of your Django app.

In order to configure Django static files for development, you need to create a “static” directory inside of your Django app. This directory will contain all the static files such as images, JavaScript, and CSS files. To do this, open your terminal and run the following command:

mkdir static
This will create a directory called “static” inside of your Django app. Once the directory is created, you can add the static files to it.

Add the following code to your views.py file:

In order to configure Django static files for development, you need to add the following code to your views.py file:

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)

This code will allow Django to serve static files from the STATIC_ROOT directory. You can find more information about configuring Django static files for development in the official Django documentation.

Add the following code to your urls.py file:

In order to configure Django static files for development, you need to add the following code to your urls.py file:

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)

This code will enable Django to serve static files from the STATIC_ROOT directory. You can find more information about configuring Django static files for development in the official Django documentation.

Run the following command in your terminal:

In order to configure Django static files for development, you need to run the following command in your terminal:

python manage.py collectstatic
This command will collect all the static files from your Django app and put them in the directory you specified in your settings.py file. After running this command, you should be able to access your static files from the browser.

Finally, add the following code to your base.html file:

In order to configure Django static files for development, you need to add the following code to your base.html file:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">

This code will allow you to access the static files in your Django app. You can also use the {% static %} tag to link to other static files such as images, JavaScript, and other media. For more information on how to configure Django static files for development, please refer to the official Django documentation.

Useful Links