Django Academy · Aula

STATICFILES e a tag static

Organize e referencie seus recursos

Aula 1 de 413 etapas

STATICFILES e a tag static é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

What Static Files Are

Your CSS, JavaScript, and design images do not change per request. Django calls these static files and serves them separately from your Python views. 🎨

The static App

Django ships django.contrib.staticfiles in INSTALLED_APPS by default. It finds, collects, and serves your assets so you do not wire that up by hand.

INSTALLED_APPS = [
    'django.contrib.staticfiles',
]

Where Files Live

By convention you keep an app's assets inside a folder named static right next to its views and templates. Django scans every app for that folder.

blog/
  static/
    blog/
      style.css

Namespace Your Folder

Nest assets under a folder matching the app name, like static/blog/. This namespacing stops two apps from clobbering each other's style.css.

The STATIC_URL Setting

The STATIC_URL setting is the public path prefix browsers use to fetch assets. Requests starting with this prefix are routed to your static files.

STATIC_URL = '/static/'

Load the static Tag

To reference assets in a template, first load the tag library with load static at the very top of the file.

{% load static %}

Use the static Tag

The static tag turns a relative asset path into the correct full URL, so you never hardcode STATIC_URL into your HTML.

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

Why Not Hardcode

If you wrote /static/blog/style.css by hand, changing STATIC_URL later would break every page. The static tag keeps your links flexible. ✅

Linking JavaScript

The same static tag works for any asset type. Point a script tag at a JS file and Django resolves the path for you.

<script src="{% static 'blog/app.js' %}"></script>

STATICFILES_DIRS

For project-wide assets that belong to no single app, add their folders to STATICFILES_DIRS and Django will search there too.

STATICFILES_DIRS = [BASE_DIR / 'assets']

Finders Do the Work

Behind the scenes, static finders walk every app's static folder plus STATICFILES_DIRS to locate the file a template asks for.

Quick Check

Let us check how you reference a static file in a template.

Recap

You learned that static files are your CSS and JS, that they live in namespaced static folders, and that the static tag builds their URLs cleanly. 🎉

Grátis para começar

Aprenda Python com um tutor de IA — grátis

Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.

Cursos
30
Aulas
120

Perguntas Frequentes

A aula “STATICFILES e a tag static” é grátis?

Sim — o texto completo de “STATICFILES e a tag static” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.

O que vou aprender em “STATICFILES e a tag static”?

Organize e referencie seus recursos Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Django Academy?

Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “STATICFILES e a tag static”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Django Academy?

Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. STATICFILES e a tag static
  2. collectstatic para produção
  3. MEDIA_ROOT e uploads de arquivos
  4. ImageField e disponibilização de uploads
← Voltar para Django Academy