0Pricing
Django Academy · บทเรียน

STATICFILES และแท็ก static

จัดระเบียบและอ้างอิงไฟล์ทรัพยากรของคุณ

STATICFILES และแท็ก static เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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. 🎉

คำถามที่พบบ่อย

บทเรียน “STATICFILES และแท็ก static” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “STATICFILES และแท็ก static” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “STATICFILES และแท็ก static”

จัดระเบียบและอ้างอิงไฟล์ทรัพยากรของคุณ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “STATICFILES และแท็ก static” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. STATICFILES และแท็ก static
  2. collectstatic สำหรับใช้งานจริง
  3. MEDIA_ROOT และการอัปโหลดไฟล์
  4. ImageField และการให้บริการไฟล์อัปโหลด
← กลับไปที่ Django Academy