문제

So I'm having a bit of trouble. I get a 404 when I try to visit the url for a certain model:

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

So the top url will be something like rewards/amazon-gift card, while the bottom url would be something like rewards/amazon (to show all of the gift cards that amazon has to offer). The reward url works as expected, but I get a 404 when I try to visit the bottom url. The view:

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

What's going on here?

도움이 되었습니까?

해결책

Your patterns for the two views are identical, so the CompanyDetail view can never be called. Instead, the pattern for RedeemReward matches all slugs and raises a 404 for slugs that do not match whatever its model class is. (Probably Reward.) Put something in the URLs to differentiate company URLs from reward URLs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top