python - 找不到网址 Django

标签 python django templates view urlconf

所以我遇到了一些麻烦。当我尝试访问某个型号的 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/

相关文章:

Python、Django 1.7 : Redirect all URLs to a single controller

django - 如何使用 NGINX 和 Gunicorn 查看 Django 错误

c++ - 使用模板比较器的完整示例

python - Pandas:如何将系列的多重索引折叠为日期时间索引?

python - 遍历列表中的字符串

Django模板条件变量赋值

c++ - 函数模板的显式实例化失败 (g++)

c++ - Variadic 模板候选者不匹配

python - SQLAlchemy 中是否有与 django 的管理器等效的东西?

Python ctypes - dll 函数接受结构崩溃