【发布时间】:2013-02-04 02:04:26
【问题描述】:
我正在尝试调用基于类的视图并且我能够做到,但由于某种原因我没有得到我正在调用的新类的上下文
class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):
template_name = "accounts/thing.html"
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(ShowAppsView, self).dispatch(*args, **kwargs)
def get(self, request, username, **kwargs):
u = get_object_or_404(User, pk=self.current_user_id(request))
if u.username == username:
cities_list=City.objects.filter(user_id__exact=self.current_user_id(request)).order_by('-kms')
allcategories = Category.objects.all()
allcities = City.objects.all()
rating_list = Rating.objects.filter(user=u)
totalMiles = 0
for city in cities_list:
totalMiles = totalMiles + city.kms
return self.render_to_response({'totalMiles': totalMiles , 'cities_list':cities_list,'rating_list':rating_list,'allcities' : allcities, 'allcategories':allcategories})
class ManageAppView(LoginRequiredMixin, CheckTokenMixin, CurrentUserIdMixin,TemplateView):
template_name = "accounts/thing.html"
def compute_context(self, request, username):
#some logic here
if u.username == username:
if request.GET.get('action') == 'delete':
#some logic here and then:
ShowAppsView.as_view()(request,username)
我做错了什么?
【问题讨论】:
-
这应该是做什么的?您希望通过简单地调用该视图来实现什么?我猜您可能需要返回调用它的结果,但由于
compute_context是一种非标准方法,因此很难确定。 -
我有点“刷新”我的页面,所以我用一些新的上下文数据回忆我的上一页
-
我正在返回 return self.render_to_response(self.compute_context(request,username))
-
但是您还没有通过简单地调用该视图来解释您希望发生的事情。你调用它然后丢弃响应:正如我所说,你真的想返回它吗?或者,也许您打算调用它的 compute_context 方法并使用它返回的值做一些事情?事实上,这条线完全没有意义。
-
因为你调用它然后对结果不做任何事情。
标签: python django django-class-based-views