如何获得匹配的网址格式?

时间:2018-07-01 11:02:53

标签: html django python-3.x

我正在努力将网址与pk以外的参数匹配。我有一个具有年份和会话属性的季节模型:

 class Season(models.Model):
     season_choices = SEASON_CHOICES
     current = models.BooleanField(default=False)
     year = models.IntegerField(_('year'), choices=year_dropdown, default=datetime.datetime.now().year)
     session = models.CharField(max_length=100, null=True, blank=True, choices=season_choices)

,我想匹配网址,例如季节/年份/会话。 我有一个列出所有季节的列表视图:

 class SeasonList(ListView):
     model = Season
     template_name = 'team/season_list.html'

     def get_queryset(self):
         s = SeasonModel.objects.filter()
         return s

和应该显示季节细节的局部视图。

  class SeasonDetail(DetailView):
     model = Season
     template_name = 'team/season_detail.html'

     def get_object(self, *args, **kwargs):
         season = get_object_or_404(Season, year=self.kwargs['year'], session=self.kwargs['session'])
         return season
  • 我最初并没有包含这个UpdateView,它最初被称为Season,所以这是错误的一部分。

     class SeasonUpdate(UpdateView):
     model = Season
     template_name = 'team/season.html'
     form_class = SeasonForm
     success_url = 'success'
    
     def get_object(self, queryset=None):
         obj = SeasonModel.objects.get(current=True)
         return obj
    
     def get_context_data(self,**kwargs):
         season = SeasonModel.objects.get(current=True)
         context = super(Season,self).get_context_data(**kwargs)
         context['games_list'] = GameModel.objects.filter(season=season).order_by('date')
         context['past_seasons'] = Seas.  onModel.objects.filter(current=False).order_by('-year')
         if self.request.POST:
        context['games'] = GameFormSet(self.request.POST)
         else:
        context['games'] = GameFormSet()
         return context
    
     def form_valid(self, form):
        context = self.get_context_data()
        games = context['games']
        with transaction.atomic():
            self.object = form.save()
    
            if games.is_valid():
                games.instance = self.object
                games.save()
        return super(Season, self).form_valid(form)
    

这是我的网址:

 url(r'^season/$', team_views.SeasonList.as_view(), name='season_list'),
 url(r'^season/(?P<year>[0-9]{4})/(?P<session>[-\w]+)/$', team_views.SeasonDetail.as_view(), name='season_detail'),

最后是我的模板链接:

 <a href="{% url 'team:season_detail' year=season.year session=season.session %}">
 <p>{{ season.year }} {{ season.session }}</p></a>
  • 这是一个for循环模板错误!现在知道了。

0 个答案:

没有答案