2014-12-06 19 views
-1

訪問部分我下面從Django的官方文檔編寫你的第一個Django應用程序,第3部分服務器錯誤(500)在Django應用程序

在本教程的一部分,我已經編輯民調/ urls.py文件是這樣的:

from django.conf.urls import url 


from polls import views 

urlpatterns = [ 

    url(r'^$', views.index, name='index'), 

    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'), 

    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'), 

    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'), 
    ] 

現在訪問結果部分,我已經進入這個鏈接:

http://localhost:8000/polls/34/results/ 

卻是露出服務器錯誤(500)

注:其它部分,如:

http://localhost:8000/polls/34/ 
http://localhost:8000/polls/34/vote/ 

工作正常。

我已經猜到有可能是語法錯誤在我的代碼。但我找不到任何。

編輯:

這裏是我的調查目錄:

polls/ 

    admin.py 

    __init__.py 

    models.py 

    tests.py 

    urls.py 

    views.py  

編輯2:

models.py:

mport datetime 

from django.db import models 
from django.utils import timezone 

# Create your models here. 
class Question(models.Model): 
    question_text = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published') 

def __unicode__(self): 
    return u'%s' % (self.question_text) 

def was_published_recently(self): 
    return self.pub_date >= timezone.now()-datetime.timedelta(days=1) 
    was_published_recently.admin_order_field = "pub_date" 
    was_published_recently.boolean = True 
    was_published_recently.short_description = "Published recently?" 

class Choice(models.Model): 
    question = models.ForeignKey(Question) 
    choice_text = models.CharField(max_length=200) 
    votes = models.IntegerField(default=0) 

def __unicode__(self): 
    return u'%s' % (self.choice_text) 

views.py:

from django.shortcuts import render 
from django.http import HttpResponse 

# Create your views here. 

def index(request): 
    return HttpResponse("Hello, world. You're at the polls index.") 

def detail(request, question_id): 
    return HttpResponse("You're looking at question %s." % question_id) 

def results(request, question_id): 
    response = "You're looking at the results of question %s." 
    return HttpRespones(response % question_id) 

def vote(request, question_id): 
    return HttpResponse("You're voting on question %s." % question_id) 

admin.py:

from django.contrib import admin 
from polls.models import Choice, Question 

# Register your models here. 
class ChoiceInline(admin.TabularInline): 
    model = Choice 
    extra = 3 

class QuestionAdmin(admin.ModelAdmin): 
    fieldsets= [ 
    (None, {'fields': ['question_text']}), 
    ('Date information', {'fields':['pub_date'], 
          'classes': ['collapse']}), 
    ] 
    inlines = [ChoiceInline] 
    list_display = ('question_text', 'pub_date', 
    'was_published_recently') 
    list_filter = ['pub_date'] 
    search_fields = ['question_text'] 

    admin.site.register(Question, QuestionAdmin) 

的mysite/setting.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '_bl4&0u5ph5=1l**)*[email protected]$fwj=abt4frj5#6' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = False 

TEMPLATE_DEBUG = True 

ALLOWED_HOSTS = ['localhost'] 


# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'polls', 
) 

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'mysite.urls' 

WSGI_APPLICATION = 'mysite.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/dev/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'mydatabase', 
     'USER': 'root', 
     'PASSWORD': '', 
     'HOST': 'localhost', 
     'PORT': '5432', 
    } 
} 

# Internationalization 
# https://docs.djangoproject.com/en/dev/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'Asia/Dhaka' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

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

# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/dev/howto/static-files/ 

STATIC_URL = '/static/' 
+0

您可以將調查模型添加到此? – wkcamp 2014-12-06 15:20:16

+1

通常服務器日誌/控制檯中的輸出不僅僅是500錯誤。 – akonsu 2014-12-06 15:20:57

+0

@WillCampbell .. **投票模式** **指民調/ models.py ** **或目錄地圖嗎?**我很抱歉,如果這困擾你,我是一個很初級。 – ni8mr 2014-12-06 15:24:36

回答

1

只是一個小錯誤,但如果這個固定它,我會好好回答:): 你在馬車方法返回HttpRespones,而不是return HttpResponse。順便說一句,如果您使用的是「活」的服務器,而不是Django的玩具,你應該看到一個SyntaxError在日誌中,例如在Mac上的/var/log/apache2/error_log

0

您設置DEBUG在settings.py假,但你並沒有改變ALLOWED_HOSTS變量,包括您的域名。作爲一項安全措施,只要您關閉了調試功能,就必須明確說明允許從哪個域名中調用該應用程序。

ALLOWED_HOSTS = ['yourdomainname.com']