在Django中导入模块(新手)

2024-09-27 18:14:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python和Django都是新手,我确信这是一件非常简单的事情。我使用PyCharm作为我的ide,并试图遵循快速入门指南[这里][1]。我根据教程设置了一个虚拟环境。在

这个项目是“DjangoProjectApp”,应用程序是“午餐”

当下面显示的文件和浏览器指向http://localhost:8000/admin/时,我得到错误:

ImportError at /admin/
No module named 'DjangoProjects.Lunch'

但是如果我在网址.py那就行了。导入此模块的正确方法是什么?谢谢。在

在网址.py公司名称:

^{pr2}$

在视图.py在

from django.shortcuts import render

# Create your views here.
from django.shortcuts import render_to_response


def index(request):
    return render_to_response('index.html')

在索引.html在

hello world!

在设置.py在

"""
Django settings for DjangoProjects project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'd^d9v4j(1maq-&_8^a+kgicmagxwbv*9m$!2st&vqz$5_h$441'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Lunch',
    'django.contrib.admin',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'DjangoProjects.urls'

WSGI_APPLICATION = 'DjangoProjects.wsgi.application'


# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/

STATIC_URL = '/static/'

Tags: djangopyhttpsdevcomtruedocssettings
1条回答
网友
1楼 · 发布于 2024-09-27 18:14:57

将该行替换为:

url(r'^lunch/$', 'Lunch.views.index')

不需要指定项目的名称。你应该从应用程序名称开始。在

另外,请确保Lunch在设置文件的INSTALLED_APPS中。在

相关问题 更多 >

    热门问题