我是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 提前感谢您的帮助......
答案 0 :(得分:-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)
我希望每当我创建一个新的Profil时,会创建一对情侣,其中p1 =我刚创建的Profil,而p2 =使用&#34;&#34;而不是名字。但是我不知道怎么做,因为有两个外键...