关于main_project项目中base.css文件存放位置的技术咨询
base.css in Your Django Project Hey there! Great question—let’s clear this up with Django’s standard conventions:
Don’t put CSS files in the
templatesdirectory
Thetemplatesfolder is strictly for HTML template files (like yourbase.html). Static assets such as CSS, JavaScript, or images don’t belong here—mixing them will make your project messy and could cause issues with how Django serves static files later, especially when you deploy to production.Create a
staticdirectory inmain_projectinstead
This is the recommended, industry-standard approach for Django projects. Your folder structure should end up looking like this:main_project/ ├── templates/ │ └── base.html ├── static/ │ └── base.css
Quick Setup Tips to Make It Work
Verify your
settings.pyconfig
Make sure these settings are present (they’re usually included by default, but it’s good to double-check):STATIC_URL = '/static/' # Add this if you're using project-level static files (not tied to a specific app) STATICFILES_DIRS = [ BASE_DIR / "static", ]Link the CSS in
base.html
Use Django’s{% static %}template tag to reference your stylesheet. Don’t forget to load the static tag at the top of your template:{% load static %} <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="{% static 'base.css' %}"> <!-- Other head content --> </head> <!-- Rest of your template --> </html>
Following this structure keeps your project organized, aligns with Django’s best practices, and ensures your static files work smoothly in both development and production.
内容的提问来源于stack exchange,提问作者Justin

