尝试将2个模型纳入我的"赛季"我的django网站上的视图/模板。目前,我得到以下作为ValueError"视图webapp.views.season没有返回HttpResponse对象。它返回了None。"不确定我做错了什么,但希望有人可以看看。
views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView
from .models import Player, Season
def home(request):
seasons = Season.objects.order_by('sid')
return render(request, 'webapp/home.html', {'seasons': seasons})
def player(request, pk):
player = get_object_or_404(Player, pk=pk)
return render(request, 'webapp/player.html', {'player': player})
def season(ListView, pk):
model = Season
template_name = 'webapp/season.html'
def get_context_data(self, **kwargs):
context = super(season, self).get_context_data(**kwargs)
context['players'] = Player.objects.all()
return context
def seasons(request):
seasons = Season.objects.order_by('sid')
return render(request, 'webapp/seasons.html', {'seasons': seasons})
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^player/(?P<pk>\d+)/$', views.player, name='player'),
url(r'^season/(?P<pk>\d+)/$', views.season, name='season'),
url(r'^seasons/$', views.seasons, name='seasons'),
]
应该注意的是,我最初有一个TypeError,表示&#34; season()得到了一个意想不到的关键字参数&#39; pk&#39;&#34;在我将pk添加到赛季论证之前。任何帮助是极大的赞赏!谢谢!
答案 0 :(得分:1)
观看次数:home
,player
和seasons
是Function Based Views,这是旧的Django Views风格。另一方面,ListView
是Class Based View,是在Django中编写视图的新方法。你混合了两种观点,这是一个坏主意。不知道你的season
视图应该做什么,但尝试类似:
def season(request, pk):
season = get_object_or_404(Season, pk=pk)
return render(
request,
'webapp/season.html',
{'season': season, 'players': Player.objects.all()}
)
答案 1 :(得分:0)
在网址中,您需要调用基于类的视图,例如views.season.as_view()