html - 如何让这个 url 模式匹配?
问题描述
我正在努力将 url 与 pk 以外的 args 匹配。我有一个具有属性年份和会话的季节模型:
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
和一个应该显示季节细节的detailview。
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
我最初没有包含这个最初称为 Season 的 UpdateView,所以这是错误的一部分。
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循环模板错误!现在想通了。
解决方案
推荐阅读
- python - 仅使用字符串方法匹配后跟数字的字符串
- javascript - 控制 geojson 图层中的图层顺序(变量中的多个图层)
- r - 在包含通配符和转义符的字符串中查找模式
- matlab - 在傅里叶空间/去噪中分离两个(图像)信号
- c++ - 静态编译的纯标准 C++ 程序是否可移植提供在同一架构上运行?
- python - Django admin 中的 GenericForeignKey 内联
- javascript - 我是否使用了错误的 Ramda 函数,R.reduce 似乎没有工作
- python - 在 Docker 中安装 azure-storage
- java - 使用 Gitlab CI 和 Junit 测试媒体播放器
- javascript - 使用 jQuery 检查数据库的用户名