我是Django的完整初學者,我嘗試沒有成功創建我的第一個只有'Hello world'的django網頁。我一直試圖通過在互聯網上查找解決我的問題幾個小時,但沒有解決方案爲我工作。這裏是我的代碼:我希望我可以幫到你...Django:ImportError;沒有名爲urls的模塊
這裏是我的目錄樹:
./ProjetArbre: AppliPageArbre arbre.db 初始化的.py manage.py ProjetArbre
./ProjetArbre/AppliPageArbre: admin.py 初始化的.py models.py tests.py views.py admin.pyc 初始化 pyc文件models.pyc urls.py
./ProjetArbre/ProjetArbre: 初始化的.py settings.py urls.py wsgi.py 初始化 pyc文件settings.pyc urls.pyc wsgi.pyc
這裏是ProjetArbre/AppliPageArbre/urls.py :
from django.conf.urls.defaults import patterns,url
from views import home
urlpatterns=patterns('',
url(r'^home/$',home,name="home")
)
ProjetArbre/AppliPageArbre/views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request) :
#return HttpResponse("HelloWorld")
print 'HelloWorld'
ProjetArbre/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ProjetArbre.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^AppliPageArbre/', include('ProjetArbre.AppliPageArbre.urls')),
)
最後ProjetArbre/ProjetArbre/settings.py:
""" Django settings for ProjetArbre project.
For more information on this file, see https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see https://docs.djangoproject.com/en/1.6/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/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'v%!t3(w*fr)[email protected]+o2(_0*d0&[email protected]^'
# 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',
'AppliPageArbre',)
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 = 'urls' #simplement urls ?
WSGI_APPLICATION = 'ProjetArbre.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'arbre.db'),
} }
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'fr-fr'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
我得到http://localhost:8000/AppliPageArbre/home的錯誤是:
ImportError at /AppliPageArbre/home
No module named urls
Request Method: GET
Request URL: http://localhost:8000/AppliPageArbre/home
Django Version: 1.6
Exception Type: ImportError
Exception Value:
No module named urls
Exception Location: /home/eisti/Dev/Environnement/local/lib/python2.7/site-packages/django/utils/importlib.py in import_module, line 40
Python Executable: /home/eisti/Dev/Environnement/bin/python
Python Version: 2.7.12
Python Path:
['/home/eisti/Dev/ProjetArbre',
'/home/eisti/Dev/Environnement/lib/python2.7',
'/home/eisti/Dev/Environnement/lib/python2.7/plat-x86_64-linux-gnu',
'/home/eisti/Dev/Environnement/lib/python2.7/lib-tk',
'/home/eisti/Dev/Environnement/lib/python2.7/lib-old',
'/home/eisti/Dev/Environnement/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/eisti/Dev/Environnement/local/lib/python2.7/site-packages',
'/home/eisti/Dev/Environnement/lib/python2.7/site-packages']
Server time: jeu, 19 Jan 2017 18:29:34 +0100
我使用Django 1.6 感謝您提前你的幫助...
由於您剛開始使用django,您應該使用它的支持版本,這個特定錯誤有多個重複項,當你嘗試他們的解決方案時發生? – Sayse
爲什麼在最新版本是1.10時使用Django 1.6? – Inconnu
我得到了同樣的錯誤或其他錯誤。我嘗試安裝不同版本的django,在不同的地方添加「'」,用我的項目的名稱來更改一些路徑或沒有找到任何適合我的解決方案。我使用1.6,因爲我學習的書很舊,而且他們的代碼不適用於django 1.10 – Spoon