我是Django的新手,并试图找出网址如何在Django中运行。
我的应用 urls.py
from django.conf.urls import url, patterns
import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'))
项目 urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^app/', include('app.urls')),
)
APPEND_SLASH = True
在settings.py
中设置, app 包含在 INSTALLED_APPS 中。
当我转到localhost:8000/app
时,我收到找不到页面。
现在我也无法前往localhost:8000/admin
。
RuntimeError at /admin/
maximum recursion depth exceeded
应用 views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("This is Hello")
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
APPEND_SLASH = True
# 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 = 'bfa&y-8d4^c&6a4hqz^am^1-xce05oj6&_$n!t=$v10fka(-!w'
# 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',
'django.contrib.admin',
'app',
)
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 = 'appmain.urls'
WSGI_APPLICATION = 'appmain.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, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/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/1.6/howto/static-files/
STATIC_URL = '/static/'
我做错了什么。请帮忙。
答案 0 :(得分:1)
您的网址没有任何问题。
在设置中:
DEBUG = True
重新加载http://localhost:8000/app/
并将回溯发布到Stackoverflow。
<强>更新强>
我构建了一个Django项目并使用了您提供的代码。它运行正常。你确定http://localhost:8000/app/
给出了404吗? http://localhost:8000/
执行404,这是预期的,因为/
没有查看。
管理员也可以。要调查'最大递归',您应该提供models.py和admin.py代码。
一些建议:重命名您的项目。 appmain
(针对该项目)和app
(针对该应用)令人困惑,因为它们看起来很相似,但更重要的是appmain
不是应用而是项目。
希望它有所帮助。
答案 1 :(得分:1)
值得尝试:
将您的项目urls.py更改为以下内容:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^app/', include('project.app.urls')),
)