Django,nginx,gunicorn:为什么在某些页面上会出现Server Error(500)?

时间:2014-09-08 21:43:26

标签: python django nginx

根本不是,而是有选择地。

例如函数random_video不起作用,video_list工作。 settings.py

import os
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS, AUTHENTICATION_BACKENDS

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]

SECRET_KEY = 'secret'

DEBUG = False

TEMPLATE_DEBUG = False

ALLOWED_HOSTS = ['.mydomain.ru']

INSTALLED_APPS = ('django.contrib.admin',
                  'django.contrib.auth',
                  'django.contrib.contenttypes',
                  'django.contrib.sessions',
                  'django.contrib.messages',
                  'django.contrib.staticfiles',
                  'django.contrib.sites',
                  'django_youtube',
                  'gdata',
                  'south',
                  'index',
                  'loginza',
                  'users',
                  'embed_video',
                  'for_parse',
                  'taggit',
                  'gunicorn',
)

SITE_ID = 1

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',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'myproject.urls'

WSGI_APPLICATION = 'myproject.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',  # Set to empty string for localhost.
        'PORT': '',  # Set to empty string for default.
    }
}


SOUTH_MIGRATION_MODULES = {
    'taggit': 'taggit.south_migrations',
}

LANGUAGE_CODE = 'ru-ru'

TIME_ZONE = 'Europe/Moscow'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
STATIC_ROOT = '/home/myuser/projects/myproject/static/'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
}

urls.py

from django.conf.urls import patterns, url, include

urlpatterns = patterns('myapp.views',
    # list of the videos
    url(r'^videos/$', 'video_list', name="myapp_video_list"),
    #random video
    url(r'^random/$', 'random_video', name='random_video'),
)

views.py

def video_list(request):
    videos_list = Video.objects.all()
    paginator = Paginator(videos_list, 42)
    page = request.GET.get('page')
    try:
        videos = paginator.page(page)
    except PageNotAnInteger:
        videos = paginator.page(1)
    except EmptyPage:
        videos = paginator.page(paginator.num_pages)

    return render_to_response(
        "django_youtube/videos.html",
        {"videos": videos},
        context_instance=RequestContext(request),
    )

def random_video(request):
    all_video = Video.objects.count()
    video11 = Video.objects.all()[random.randint(0, all_video)]
    video_params = _video_params(request, video11.video_id)
    video_params['video'] = video11
    video11.views += 1
    video11.save(update_fields=['views'])
    return render_to_response(
        "django_youtube/video.html",
        video_params,
        context_instance=RequestContext(request)
    )

在我的项目的nginx中记录错误。

[08/Sep/2014:17:15:04 -0400] "GET /youtube/random/ HTTP/1.1" 500 38 "http://mydomain.ru/video/eQIaa3dVKjc/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.94 Chrome/37.0.2062.94 Safari/537.36"

其他人没有给出。 域和其他东西被取代。 如果您需要更多细节 - 请写信。

1 个答案:

答案 0 :(得分:0)

好吧,当我学会检查我的代码时??? 错误在于他试图获取不存在的视频! Randint函数给出了包含数字的数字。

all_video = Video.objects.count () 
video11 = Video.objects.all () [random.randint (0, all_video)] 

如果它不存在视频,那么一切都会崩溃并且不得不发出错误。但我不必处理错误页面,所以nginx发布了他通常的500。