0

我想向用户在我的 django 网站上签出的对象显示相关对象。就像推荐一样。例如,当用户在 Las vegas 中单击某个状态的对象时,我希望 Las vegas 中的其他相关对象通过侧边栏显示。

就像当用户点击一个名为“拉斯维加斯的家”的链接时,当用户被重定向到显示家的页面时,在侧边栏上显示“拉斯维加斯的其他家”希望你明白我的意思吗?我尝试了以下代码,但它不起作用。整天都在为此奋斗,但没有成功。

楷模

class Finhall(models.Model):
    user=models.ForeignKey(User)
    name=models.CharField(max_length=250, unique=True)
    address=models.CharField(max_length=200)
    city=models.CharField(max_length=200)
    state=models.CharField(max_length=200, help_text='Las vegas')

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

意见:

def homedetail(request,finhall_id,slug):
    post=Finhall.objects.get(id=finhall_id,slug=slug) #show details of an object

    stateme=Finhall.objects.get(state)  #show similar objects based on state
    booms=Finhall.objects.filter(state=stateme)
    vips=booms.select_related()
    for vip in vips:
        print vip.id
    return render_to_response('postdetail.html',{'post':post,'vips':vips,'Finhall':Finhall},context_instance=RequestContext(request))
4

1 回答 1

1

假设state派生自post您应该在模板中使用filter而不是get迭代,例如stateme

def home(request, finhall_id, slug):
    qs = Finhall.objects.all()

    try:
        finhall = qs.get(id=finhall_id, slug=slug)
    except Finhall.DoesNotExist:
        finhall = None

    if finhall:
        similar_finhalls = qs.filter(finhall.state)
    else:
        similar_finhalls = Finhall.objects.none()

    # other stuff

    return render_to_response('home.html', {
        'finhall': finhall,
        'similar_finhalls': similar_finhalls
    },context_instance=RequestContext(request))
于 2013-04-20T13:01:42.553 回答