【问题标题】:Django : ImportError ; No module named urlsDjango:导入错误;没有名为 urls 的模块
【发布时间】: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


【解决方案1】:

我通过阅读另一个关于 Django 1.10 的教程设法解决了这个问题。谢谢:)

现在,我有另一个问题:在 models.py 中,我有以下类,例如:

class Profil(models.Model):
    sexe = models.CharField(max_length=1, choices=genre_choix,default="N")
    id_profil = models.AutoField(auto_created=True, primary_key=True, serialize=False)
    famille = models.ForeignKey(Famille, default=0, blank=True,unique=False,null=True)
    prenom = models.CharField(max_length=30)
    prenom2 = models.CharField(max_length=30,blank=True)
    nom_famille = models.CharField(max_length=30)
    nom_jeune_fille = models.CharField(max_length=30,blank=True)
    Date_anniv = models.DateField(blank=True, null=True)
    mort = models.DateField(blank=True, null=True)
    travail = models.CharField(max_length=25, default="", blank=True)
    information = models.TextField(default="", blank=True)
    profil_picture = models.FileField(blank=True)

    def __str__(self):
        return str(self.prenom)



class Couple(models.Model):
    p1 = models.ForeignKey("Profil", related_name='+')
    p2 = models.ForeignKey("Profil", related_name='+')
    marriage = models.DateField(default="", blank=True,null=True)
    divorce = models.DateField(default="", blank=True,null=True)

    def __str__(self):
        return str(self.p1.prenom+"<3"+self.p2.prenom)

我希望每次创建新的个人资料时,都会使用 p1 = 我刚刚创建的个人资料和 p2 = 带有“?”的虚假个人资料创建一对而不是名字。但是由于有两个外键,我不知道该怎么做...

【讨论】:

  • 你应该永远这样做,不要更新你的答案,只是说“谢谢,我想通了”,因为其他搜索问题的人不会找到一个答案。解释你是怎么做到的。另外,不要用另一个完全不同的问题来回答你自己的问题。
猜你喜欢
  • 1970-01-01
  • 2012-08-12
  • 2018-09-04
  • 2014-05-16
  • 2017-03-13
  • 2012-03-16
  • 2012-10-26
相关资源
最近更新 更多