这可能是一个愚蠢的问题,但我只是python / django的初学者。
我一直收到服务器错误(500)和未找到页面(在此服务器上找不到请求的URL。)
在我的views.py中,如果我只测试一个字符串“Hello,World”,那就没关系了
如果我使用index.html(在Templates文件夹中)或者注册,登录等链接,我会出现上述错误。
我可以在没有静态css文件的情况下看到admin的登录窗口。当我登录时,我也会遇到Server Error 500。
我猜问题是来自Django的URL系统无法正常工作?
我得到的现有项目是基于Django 1.3.1构建的,我刚刚将其升级到1.7.4。 从下面可以看出,django 1.3.1中项目的旧结构是这样的:
django 1.3.1
server
/node/
admin.py, functions.py, models.py
/templates/
index.html
/static/
/manage.py
/settings.py
/urls.py
和1.7.4这样:
django 1.7.4
server
/server/
settings.py, urls.py, views.py
/node/
admin.py, functions.py, models.py, views.py
/templates/
index.html
/static/
/manage.py
以下是该项目的详细信息:
functions.py
@csrf_exempt
def register(request):
@csrf_exempt
def login(request):
@csrf_exempt
def forgot_password(request):
models.py
class UserProfile(models.Model):
urls.py:
from django.conf.urls import patterns, include, url
from server import views
from node.functions import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', views.index_view),
# url(r'^index/$', views.indexView),
url(r'^admin/', include(admin.site.urls)),
url(r'^register/', register),
url(r'^login/', login),
url(r'^forgot_password/', forgot_password),
)
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index_view(request):
return HttpResponse("Hello, World")
def indexView(request):
return render(request, 'index.html')
settings.py
import os
DEBUG = False
TEMPLATE_DEBUG = DEBUG
__file__ = os.getcwd()
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
ALLOWED_HOSTS = ['.example.com', '.exmaple.com.']
AUTH_PROFILE_MODULE = 'node.UserProfile'
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'db',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '',
'PORT': '',
}
}
TIME_ZONE = 'America/Chicago'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media")
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, "static")
STATIC_URL = 'http://example.com/static/'
ADMIN_MEDIA_PREFIX = '/admin/media/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
SECRET_KEY = ‘xxx'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'server.urls'
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'node',
'django.contrib.gis',
)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
# Templates directory
REGISTRATION_TEMPLATES_DIR = 'registration/'
AUTHENTICATION_BACKENDS = (
'node.auth_backends.CustomBackend',
)
GDAL_LIBRARY_PATH = '/usr/local/lib/libgdal.so'
TEST_RUNNER = 'django.test.runner.DiscoverRunner'
由于
答案 0 :(得分:1)
尝试使用DEBUG = True!
服务器错误(500)是后端错误(逻辑!),DEBUG = True会说明有关错误及其解决方案的更多信息。
答案 1 :(得分:0)
如前所述,结构已更改,因此您收到此错误,我建议您在虚拟环境中运行基于1.3构建的项目。 感谢