1

所以我有点麻烦。当我尝试访问某个型号的 url 时收到 404:

url(r'^rewards/(?P<slug>[-\w]+)/$', RedeemReward.as_view(), name="reward"),
url(r'^rewards/(?P<slug>[-\w]+)/$', CompanyDetail.as_view(), name="company"),

因此,顶部 url 将类似于rewards/amazon-gift card,而底部url 将类似于reward/amazon(显示亚马逊必须提供的所有礼品卡)。奖励网址按预期工作,但当我尝试访问底部网址时得到 404。风景:

class CompanyDetail(DetailView):
    model = Company
    context_object_name = 'company'
    slug_field = 'company_slug'
    template_name = 'asdx/companies.html'

    def get_rewards(self):
    rewards = Reward.objects.filter(company=self.object)
    return rewards

    def get_context_data(self, **kwargs):
    context = super(CompanyDetail, self).get_context_data(**kwargs)
    context['rewards'] = self.get_rewards()
    return context

这里发生了什么?

4

1 回答 1

5

您对两个视图的模式是相同的,因此CompanyDetail永远不能调用该视图。相反,模式RedeemReward匹配所有 slug,并为不匹配其模型类的 slug 引发 404。(可能Reward。)在 URL 中添加一些内容以区分公司 URL 和奖励 URL。

于 2013-12-29T01:17:13.663 回答