2014-03-27 46 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