0

公式チュートリアルからdjangoを学ぼうとしています。

私は奇妙な問題で立ち往生しています。それは些細なことかもしれませんが、私はそれを理解することができません -

私はこのチュートリアルに従っています:

チュートリアル 3

私の問題は、にアクセスしようとするとhttp://hello.djangoserver:8080/poll、次の出力が得られることです。

出力

しかし、出力は object の値である必要があります。何が問題なのかわかりません?? チュートリアルで説明されている以前のすべての手順は、私にとってはうまく機能しています。

コンソール出力は次のとおりです。

(InteractiveConsole)
>>> from poll.models import Question, Choice
>>> Question.objects.get(pk=1)
<Question: What's up?>
>>> quit()

/srv/www/hello/poll/views.py

# Create your views here.  
from django.http import HttpResponse
from django.template import RequestContext, loader
from poll.models import Question

def index(request):
     latest_question_list = Question.objects.order_by('-pub_date')[:5]
     template = loader.get_template('poll/index.html')
     context = RequestContext(request, {
    'latest_question_list': latest_question_list,
    })  
    return HttpResponse(template.render(context))  

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 HttpResponse(response % question_id)

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

/srv/www/hello/poll/urls.py

  from django.conf.urls import patterns, url 
  from poll import views

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

   # ex: /polls/5/
   url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
   # ex: /polls/5/results/
   url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
   # ex: /polls/5/vote/
   url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),

/srv/www/hello/hello/urls.py

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

  # Uncomment the next two lines to enable the admin:
  from django.contrib import admin
  admin.autodiscover()

  urlpatterns = patterns('',

      url(r'^poll/', include('poll.urls')),

/srv/www/hello/poll/templates/poll/index.html

  {% if latest_question_list %}
  <ul>
  {% for question in latest_question_list %}
    <li><a href="/poll/{
   question.id }}/">{
  question.question_text }}</a></li>
  {% endfor %}
  </ul>
  {% else %}
     <p>No polls are available.</p>
  {% endif %}
  }"
  }"

settings.py からのいくつかの関連する構造 -

/srv/www/hello/hello/settings.py:

  DEBUG = True

  DATABASES = { 
  'default': {
     'ENGINE': 'django.db.backends.mysql', 
             # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
     'NAME': 'mysite_db',                      
             # Or path to database file if using sqlite3.
    # The following settings are not used with sqlite3:
    'USER': 'root',
    'PASSWORD': '<my password>',
    'HOST': '',                      
             # Empty for localhost through domain sockets or 
             #'127.0.0.1' for localhost through TCP.
    'PORT': '',                      
             # Set to empty string for default.
    }   
  }


  MEDIA_ROOT = ''

  MEDIA_URL = ''

  STATIC_ROOT = ''

  STATIC_URL = '/static/'


  STATICFILES_DIRS = (
     # Put strings here, like "/home/html/static" or "C:/www/django/static".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.   
  )


  STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
  )



  TEMPLATE_LOADERS = (
   'django.template.loaders.filesystem.Loader',
   'django.template.loaders.app_directories.Loader',
    # 'django.template.loaders.eggs.Loader',
  )


  ROOT_URLCONF = 'hello.urls'

  WSGI_APPLICATION = 'hello.wsgi.application'

  TEMPLATE_DIRS = (

  )


  INSTALLED_APPS = (
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'poll',
   # Uncomment the next line to enable the admin:
   'django.contrib.admin',
   # Uncomment the next line to enable admin documentation:
   'django.contrib.admindocs',
  )

/srv/www/hello/poll/models.py :

from django.db import models

from django.utils import timezone

import datetime


# Create your models here.

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


def __unicode__(self):  # Python 3: def __str__(self):
    return 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?'

サーバー実行コマンド:

    $ python manage.py runserver hello.djangoserver:8080
4

2 に答える 2

2

HTML テンプレートで と を使用{{ question.id }}{{ question.question_text }}ます。double{{と に注意してください}}

于 2013-10-22T21:17:04.887 に答える
2

/srv/www/hello/poll/templates/poll/index.html の中括弧の 1 つを忘れています。

于 2013-10-22T21:17:17.953 に答える