使用Microsoft IIS发布Django项目失败:AttributeError错误求助
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
- Open IIS Manager and navigate to your Django site.
- Go to FastCGI Settings (under the site's features).
- Find the Python entry associated with your deployment and click Edit.
- In the Environment Variables section, locate
WSGI_HANDLER:- Replace its value with
your_project_name.wsgi.application(replaceyour_project_namewith the actual name of your Django project folder, the one containingsettings.pyandwsgi.py).
- Replace its value with
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 beyour_project_name.settings(matches the value inwsgi.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 atC:\sites\myproject, set PYTHONPATH toC:\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

