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

不使用Django Admin且搭建静态页面网站,启动项目需执行migrate吗?

Do You Need to Run migrate for a Static Django Site Without Admin?

Great question! Let's break this down clearly:

  • Short answer: No, you don't have to run migrate if you're building a purely static site that never touches the database and doesn't use Django Admin or any database-dependent built-in features.

  • Why migrate exists in the first place: The migrate command syncs Django model changes to your database. By default, new Django projects include apps like django.contrib.auth, django.contrib.sessions, and django.contrib.contenttypes in INSTALLED_APPS—these rely on database tables to work. But if you don't need user authentication, session tracking, or any other feature that uses a database, you can skip this step entirely.

  • Important caveat: If you leave those database-dependent apps in your INSTALLED_APPS but don't run migrate, you'll get errors when starting the server. Django will try to access tables that don't exist. So you have two solid options here:

    1. Strip down your INSTALLED_APPS: Remove all apps that require a database (like auth, sessions, admin, contenttypes). Keep only what you need for static files, like django.contrib.staticfiles, plus your own custom app. This way, you don't need a database at all.
    2. Run migrate anyway: If you want to keep some of those default apps (even if you don't use Admin), you'll need to run migrate to create the necessary database tables. For example, if you're using sessions for some edge case (unlikely for a static site), this is necessary.
  • Example setup for a static-only site: Your INSTALLED_APPS might look like this:

    INSTALLED_APPS = [
        'django.contrib.staticfiles',
        # Your custom static site app here, e.g., 'my_static_pages',
    ]
    

    With this setup, you can run python manage.py runserver directly without ever touching migrate or setting up a database.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:18:42