所以我遇到了一些麻烦。当我尝试访问某个型号的 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 将类似于 rewards/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
这是怎么回事?
最佳答案
这两个 View 的模式相同,因此永远无法调用 CompanyDetail
View 。相反,RedeemReward
的模式匹配所有 slug,并为与其模型类不匹配的 slug 引发 404。 (可能是 Reward
。)在 URL 中添加一些内容以区分公司 URL 和奖励 URL。
关于python - 找不到网址 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821154/