Django Tutorial: Serving Files in Production πŸ“

beginner
13 min

Django Tutorial: Serving Files in Production πŸ“

Welcome to our comprehensive guide on serving files in Django for production! In this tutorial, we will learn how to serve static files like CSS, JavaScript, images, and media files in your Django application. Let's dive in!

Static Files in Django 🎯

Static files are important for enhancing the visual appearance and functionality of your web application. Django provides a dedicated system to manage static files efficiently.

Static URL Patterns πŸ“

To serve static files, you need to define URL patterns in your Django project. Here's how you can do it:

  1. First, create a new directory called static inside your app directory (e.g., my_app).
my_project/ my_app/ static/
  1. Next, in your app's urls.py, include the static files URL pattern:
python
from django.contrib import admin from django.urls import path, include from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), path('', include('my_app.urls')), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  1. In your project's settings.py, set the STATIC_URL and STATIC_ROOT variables:
python
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  1. Collect static files using the command python manage.py collectstatic. This will gather all static files in the specified STATIC_ROOT directory.

Accessing Static Files πŸ’‘

Now, you can access your static files by using the URL pattern you defined in the previous step. For example, if you have an image named logo.png inside the static/my_app/ directory, you can access it by visiting http://localhost:8000/static/my_app/logo.png in your browser.

Media Files in Django 🎯

Media files, such as user uploaded images or videos, can be managed using Django's built-in models.FileField and models.ImageField.

  1. Create a new model in your app's models.py:
python
from django.db import models class MyModel(models.Model): title = models.CharField(max_length=100) image = models.ImageField(upload_to='my_app/images')
  1. In your app's urls.py, define a URL pattern to handle media file downloads:
python
from django.conf.urls import url from django.views.static import serve from my_app.models import MyModel urlpatterns = [ # ... url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), ]
  1. In your project's settings.py, set the MEDIA_URL and MEDIA_ROOT variables:
python
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  1. Run the migrations to create the database table for your model:
python manage.py makemigrations python manage.py migrate
  1. Now you can upload media files to your Django application using the Django admin site or custom forms.

Quiz Time πŸ’‘

Quick Quiz
Question 1 of 1

To serve static files, you should first define URL patterns in which file?

Quick Quiz
Question 1 of 1

To handle media file downloads, you should define a URL pattern pointing to which view function?