2014-03-27 49 views
0

我对Python和Django都很陌生,我正在努力解决这个问题,这是一件非常简单的事情。我使用PyCharm作为我的IDE,并试图遵循快速入门指南[这里] [1]。按照教程设置虚拟env。在Django中导入模块(新手)

该项目是“DjangoProjectApp”和应用程序是“午餐”

随着文件如下图所示,在浏览器指着http://localhost:8000/admin/我得到的错误:

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

但是,如果我注释掉urls.py中的第二个url路径,然后它可以工作。什么是我导入这个模块的正确方法?谢谢。

urls.py:

from django.conf.urls import patterns, include, url 
from django.contrib import admin 

urlpatterns = patterns('', 
    # Examples: 
    # url(r'^$', 'DjangoProjects.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 

    url(r'^admin/', include(admin.site.urls)), 
    url(r'^lunch/$', 'DjangoProjects.Lunch.views.index') 
) 

views.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') 

的index.html

hello world! 

settings.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/' 
+0

尝试用'Lunch.views.index'替换'DjangoProjects.Lunch.views.index'。 – alecxe

+0

谢谢。这确实修复了模块未发现的错误。我认为我最初添加完全限定名的原因是,没有它,它会抛出另一个错误,指出TemplateDoesNotExist。 “索引”绝对是在views.py中定义的,所以你知道它为什么找不到它? – Gadzooks34

+0

检查您的[TEMPLATE_DIRS](https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs)设置。 – alecxe

回答

0

将该行替换为:

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

无需指定项目名称。您应该从应用程序名称开始。

此外,请确保Lunch位于设置文件中的INSTALLED_APPS