【发布时间】:2017-06-04 08:48:26
【问题描述】:
我是 Django 的一个完整的初学者,我尝试创建我的第一个 Django 网页,上面只有“Hello world”,但没有成功。我已经尝试了几个小时通过查看互联网来解决我的问题,但没有一个解决方案对我有用。这是我的代码:我希望我能在这里得到帮助......
这是我的目录树:
./ProjetArbre: AppliPageArbre arbre.db init.py manage.py ProjetArbre
./ProjetArbre/AppliPageArbre: admin.py init.py models.py tests.py views.py admin.pyc init.pyc models.pyc urls.py
./ProjetArbre/ProjetArbre: init.py settings.py urls.py wsgi.py init.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)0n_!98=se_o_+@+o2(_0*d0&pu19@z5g1hxz13^'
# 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,你应该使用它的支持版本,这个特定的错误有多个重复,当你尝试他们的解决方案时发生了什么?
-
为什么你使用 Django 1.6 而最新的是 1.10?
-
1.6 版本非常过时,不再受支持,您应该使用最新版本,因为它具有更多功能、更少错误和更易于使用,因此痛苦更少,并且还将获得更多帮助和资料互联网。
-
您会发现自己在使用 1.6 进行学习,因为在 1.6 和 1.7 之间,django 用于其数据库 ORM 的内容发生了巨大变化。
-
设置应该有
ROOT_URLCONF = 'ProjetArbre.urls'。请查找至少支持 Django 1.8 的另一本书或教程。否则你会遇到很多问题。
标签: python django python-2.7