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

遵循Django 2.2官方教程时出现ModuleNotFoundError: No module named 'polls'错误

Fixing Django's ModuleNotFoundError: No module named 'polls'

Hey, let's get that polls app up and running! That error means Django can't locate your polls module, so let's walk through the most common fixes:

1. Double-check your project structure

First off, make sure your polls app is in the right place and has all the required files. Your project should follow this structure (the key detail is that polls/ sits at the same level as manage.py):

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        # other core Django system files
    polls/
        __init__.py  # This empty file is critical—Python needs it to recognize the folder as a module
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        views.py
        urls.py  # Your custom URL config here

If you manually created the polls folder, you might have forgotten these __init__.py files—add empty versions of them if they're missing.

2. Confirm your INSTALLED_APPS setup

You already added 'polls' to INSTALLED_APPS in settings.py, which is correct as long as the folder structure above is right. If polls were nested inside another subfolder, you'd need to use a path like 'mysite.polls', but that's not necessary here.

3. Check where you're running the server command

You used py mysite\manage.py runserver, which means you're executing the command from the parent folder of mysite/. That works fine, but only if polls/ is inside mysite/ (next to manage.py). If polls lives outside that folder, Django won't be able to find it.

A simpler approach is to move into the mysite/ folder first, then run the server:

cd mysite
py manage.py runserver

This ensures Django looks for apps in the correct directory by default.

4. Recreate the app with Django's startapp command (if needed)

If you built the polls folder manually, you might be missing some auto-generated files that Django expects. Save your existing views.py and urls.py code, delete the current polls folder, then run this command from the same directory as manage.py:

py manage.py startapp polls

Then paste your saved code back into the new polls folder—this guarantees the structure is 100% Django-friendly.

Quick side note: Fix that HTML typo

While it's not causing the module error, your views.py has a tiny HTML mistake: <h1><this is a test page</h1> has an extra < before "this". Change it to <h1>This is a test page</h1> so your page renders correctly once the server runs.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:07:49