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

使用Microsoft IIS发布Django项目失败:AttributeError错误求助

Fixing "AttributeError: module 'django.core' has no attribute 'wsgi'" in IIS Django Deployment

Hey there, let's work through this frustrating deployment issue. That error you're seeing is almost always due to an incorrect WSGI_HANDLER configuration in your IIS FastCGI settings—here's how to fix it step by step:

Why This Happens

In Django 3.x (including your 3.1.7 version), you shouldn't point directly to django.core.wsgi for your WSGI handler. The correct handler lives in your project's auto-generated wsgi.py file, which wraps Django's core WSGI setup properly. The error pops up because the FastCGI module (wfastcgi) is trying to access django.core.wsgi directly, which isn't the right entry point.

Step 1: Correct the WSGI_HANDLER Environment Variable

  1. Open IIS Manager and navigate to your Django site.
  2. Go to FastCGI Settings (under the site's features).
  3. Find the Python entry associated with your deployment and click Edit.
  4. In the Environment Variables section, locate WSGI_HANDLER:
    • Replace its value with your_project_name.wsgi.application (replace your_project_name with the actual name of your Django project folder, the one containing settings.py and wsgi.py).

Step 2: Verify Your Project's wsgi.py File

Make sure your project's wsgi.py (in the root project folder) has the correct default content—this is what connects Django to the WSGI server:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings')

application = get_wsgi_application()

Double-check that your_project_name.settings matches your actual settings module path.

Step 3: Confirm Other Critical Environment Variables

While you're in the FastCGI settings, ensure these variables are also set correctly:

  • DJANGO_SETTINGS_MODULE: Should be your_project_name.settings (matches the value in wsgi.py).
  • PYTHONPATH: Should point to your project's root directory (the folder containing your project folder, not the project folder itself—e.g., if your project is at C:\sites\myproject, set PYTHONPATH to C:\sites).

Step 4: Permissions & Restart

  • Make sure the IIS application pool identity (usually IIS AppPool\YourAppPoolName) has read access to your project folder and Python installation directory.
  • Restart your IIS application pool and the site itself to apply all changes.

After following these steps, try accessing http://localhost again—this should resolve the AttributeError and let IIS properly serve your Django project.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 09:02:40