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

如何创建无自动生成注释的Django项目?

How to Create a Django Project Without Auto-Generated Comments

Great question—like you, I’ve found those default comments in settings.py and urls.py to be unnecessary clutter once you’re familiar with Django, and manually cleaning them up every time is a total pain. Unfortunately, Django doesn’t have a built-in --no-comment flag for startproject (as of the latest stable version) to skip generating those comments directly. But there are two solid workarounds to automate this and save you the repetitive work:

1. Use a Custom Project Template (One-Time Setup, Permanent Solution)

Django’s startproject command relies on template files to generate your project structure. You can create your own stripped-down template with zero comments, then reuse it for every new project. Here’s how to do it:

  • First, locate Django’s default project template. On most systems, this lives in your Python’s site-packages directory, e.g., path/to/your/python/site-packages/django/conf/project_template.
  • Copy this entire directory to a location you control (like ~/django-templates/no-comment-project).
  • Go through every file in your copied template (especially settings.py and urls.py) and delete all the auto-generated comments, keeping only the functional code.
  • Now, whenever you want to start a new comment-free project, run:
    django-admin startproject my_new_project --template=~/django-templates/no-comment-project
    

This will generate your project using your clean template—no comments, no post-creation cleanup needed.

2. Automate Comment Cleanup with a Script

If creating a custom template feels like overkill, you can write a simple script to strip comments from your project files right after creation. This avoids manual work and works with any new project.

Here’s a Python script that safely removes comments (while preserving strings that contain # characters, so you don’t break code like DEBUG = True # Keep this False in production):

import os
import re

def strip_comments(file_path):
    with open(file_path, 'r') as f:
        content = f.read()
    
    # Regex to match comments outside of strings (ignores # inside quotes)
    cleaned_content = re.sub(
        r'(?<!["\'])#.*',
        '',
        content
    )
    
    # Remove empty lines left by comment removal
    cleaned_content = '\n'.join([line for line in cleaned_content.split('\n') if line.strip()])
    
    with open(file_path, 'w') as f:
        f.write(cleaned_content)

if __name__ == '__main__':
    import sys
    if len(sys.argv) != 2:
        print("Usage: python clean_django_comments.py <project_root>")
        sys.exit(1)
    
    project_root = sys.argv[1]
    target_files = [
        os.path.join(project_root, 'settings.py'),
        os.path.join(project_root, 'urls.py'),
        # Add other files you want to clean here (e.g., wsgi.py)
    ]
    
    for file in target_files:
        if os.path.exists(file):
            strip_comments(file)
            print(f"Cleaned comments from {file}")
        else:
            print(f"Warning: {file} not found")

Save this as clean_django_comments.py, then after creating a project:

python clean_django_comments.py my_new_project

To make it even easier, wrap this into a shell script (for Linux/macOS) to combine project creation and cleanup into one command:

#!/bin/bash
if [ $# -ne 1 ]; then
    echo "Usage: ./start_clean_project.sh <project_name>"
    exit 1
fi

django-admin startproject $1
python clean_django_comments.py $1
echo "Created clean project: $1"

Make it executable with chmod +x start_clean_project.sh, then run ./start_clean_project.sh my_new_project to get a comment-free project in one step.

Wrap-Up

While Django doesn’t support a --no-comment flag natively, either of these methods will eliminate the tedious manual cleanup. The custom template is the most "set-it-and-forget-it" option, while the script is great if you want flexibility without modifying Django’s default templates.

内容的提问来源于stack exchange,提问作者Pedram

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:35:19