2013-12-22 82 views
7

我是Django的新手,想弄清楚Django的网址是如何工作的。Django 1.6网址不能正常工作

我的应用程序的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 = Truesettings.py应用设置包含在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

不能当场什么不妥。您的应用是否在设置INSTALLED_APPS中?你能发布views.index吗? – allcaps

+0

您是否将'APPEND_SLASH'设置为'False'?如果是这种情况,您需要转到'localhost:8000/app /' – sk1p

+0

您是否安装了CommonMiddleware?或改变了'APPEND_SLASH'? https://docs.djangoproject.com/en/dev/ref/settings/#append-slash – iurisilvio

回答

1

你的网址没有问题。

在设置:

DEBUG = True 

刷新http://localhost:8000/app/和发布回溯至#1。

UPDATE

我建立一个Django项目,并使用您所提供的代码。它运行良好。你确定http://localhost:8000/app/给404了吗? http://localhost:8000/确实404,这是预期的,因为/没有视图。

管理员也有效。为了研究'最大递归',你应该提供models.py和admin.py代码。

一些建议:重命名您的项目。 appmain(对于该项目)和app(对于应用程序)很混乱,因为它们看起来很像,但更重要的是appmain不是一个应用程序,而是一个项目。

希望它有帮助。

+0

查看更新的问题? – ajkumar25

+0

视图没问题。你在开发服务器中使用该构建吗? – allcaps

+0

是Django自带的默认版本。 – ajkumar25

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')), 
)