1

学生データベースを設計していますが、このエラー「str」オブジェクトを呼び出すことができませんでした。エラーの場所が見つからないので、小さなアプリを貼り付けます。ありがとうございます。

 TypeError at /school/
 'str' object is not callableRequest Method: GET 
 Request URL: http://tafe.pythonanywhere.com/school/ 
 Django Version: 1.3.5 
 Exception Type: TypeError 
 Exception Value: 'str' object is not callable 
 Traceback Switch to copy-and-paste view
 /usr/local/lib/python2.7/site-packages/django/core/handlers/base.py in get_response 
 1.                    for middleware_method in self._view_middleware:1.                        response = middleware_method(request, callback, callback_args, callback_kwargs)1.                        if response:1.                            break1.1.                if response is None:1.                    try:
  111.                        response = callback(request, *callback_args, **callback_kwargs) ...

私のmodels.py

  from django.db import models
  from django.contrib import admin
  class Student(models.Model):
          first_name = models.CharField(max_length=30)
          last_name = models.CharField(max_length=30)
      age = models.BigIntegerField()
      body = models.TextField()

      def __unicode__(self):
          return self.first_name

私のviews.py

  from mysite.school.models import student
  from django.shortcut import render_to_response
  from django.http import HttpResponse

  def index(request):
          students = Student.objects.all()
          render_to_response('index.html',{'students':students})

私のindex.html

Student Database
{% if students %}
<ul>
    {% for student in students %}
    <li>{{ student }}</li>
{% endfor %}
</ul>
{% endif %}

私のURL

  from django.conf.urls import patterns,include , url 
  from django.contrib import admin
  from mysite.school.views import index
  admin.autodiscover()
  urlpatterns = patterns ('',
url(r'^$','index'),
  )
4

1 に答える 1

2

The problem is with your urls.py. It should be:

from django.conf.urls import patterns, include, url 
from django.contrib import admin
from mysite.school.views import index

admin.autodiscover()


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

To save yourself some typing, I would do:

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

which is what I think you were attempting to do in the first place. Hence the error 'str' object isn't callable.

于 2013-02-26T02:54:00.670 に答える