You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于main_project项目中base.css文件存放位置的技术咨询

Correct Placement for 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 templates directory
    The templates folder is strictly for HTML template files (like your base.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 static directory in main_project instead
    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

  1. Verify your settings.py config
    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",
    ]
    
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:22:48