创建详细页面时出错

0 投票
1 回答
833 浏览
提问于 2025-04-16 08:36

我在我的模型里有两个类,像这样:

from django.db import models

class nonprofit(models.Model):
    organization = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    website = models.URLField(max_length=120, blank=True)
    ........

    def __unicode__(self):
        return self.organization

class executive(models.Model):
    nonprofit = models.ForeignKey(nonprofit)
    name = models.CharField(max_length=200)
    title = models.CharField(max_length=200)
    salary = models.PositiveIntegerField()

    def __unicode__(self):
         return self.name

我的视图看起来是这样的:

from django.shortcuts import render_to_response, get_object_or_404
from nonprofit.models import executive

def index(request):
    executives = executive.objects.all()
    return render_to_response('nonprofit/index.html', {'executives': executives})

def detail(request, id):
    e = get_object_or_404(executive, d=id)
    return render_to_response('nonprofit/detail.html', {'executives': e})

我总是遇到一个错误:FieldError:无法将关键字 'd' 解析为字段。可选项有:id, name, nonprofit, salary, title

我真的是个新手,搞不清楚该怎么解决这个问题。我不知道为什么当 'd' 等于一个字段时,它却无法解析成字段……

1 个回答

1

这里有个拼写错误:

e = get_object_or_404(executive, d=id)

应该是:

e = get_object_or_404(executive, id=id)

撰写回答