I
I
Ilya2014-09-09 00:29:10
Django
Ilya, 2014-09-09 00:29:10

Django, nginx, gunicorn: Why do some pages give Server Error (500)?

Not at all, but selectively.
For example, the random_video function does not work, but video_list does.
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)
    )

error logs in nginx
[08/Sep/2014:17:15:04 -0400] "GET /youtube/random/ HTTP/1.1" 500 38 "mydomain/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"

The rest do nothing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2014-09-09
@FireGM

The error was that he was trying to get non-existent videos! The randint function returns a digit, inclusive.

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

And if there were non-existent videos, then everything crashed and should have given an error. But I don't have a page to handle the error, so nginx gave me the usual 500.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question