Python Django从另一个基于类的视图调用基于类的视图
我试图调用一个基于类的视图,并且我能够做到这一点,但是由于某些原因,我没有得到我调用的新类的上下文Python Django从另一个基于类的视图调用基于类的视图,python,django,django-class-based-views,Python,Django,Django Class Based Views,我试图调用一个基于类的视图,并且我能够做到这一点,但是由于某些原因,我没有得到我调用的新类的上下文 class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView): template_name = "accounts/thing.html" @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs):
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)
伙计们,我做错了什么?当您开始在python中使用时,事情会变得更加复杂,因此您很容易用继承的mixin来破坏您的上下文 你不能很清楚地说出你得到了什么样的上下文和你想要什么样的上下文(你没有定义一个新的上下文),所以很难完全诊断,但是试着重新安排你的混音的顺序
class ShowAppsView(LoginRequiredMixin, CurrentUserIdMixin, TemplateView):
这意味着LoginRequiredMixin
将是从中继承的第一个类,因此如果它具有您要查找的属性,它将优先于其他类-如果没有,则python将在CurrentUserIdMixin
中查找,依此类推
如果你真的想确定你得到了你想要的上下文,你可以添加一个覆盖,比如
def get_context(self, request):
super(<my desired context mixin>), self).get_context(request)
def get_上下文(self,request):
super(),self).获取上下文(请求)
确保您获得的上下文是您想要的来自mixin的上下文
*编辑*
我不知道您在哪里找到了compute\u context
,但它不是django属性,因此只能从ShowAppsView.get()
调用,而不会在ManageAppView
中调用
ShowAppsView.as_view()(self.request)
我必须这么做
return ShowAppsView.as_view()(self.request)
这是怎么回事?你希望通过简单地称之为观点来实现什么?我想您可能需要返回调用它的结果,但由于
compute\u context
是一种非标准方法,因此很难确定。我正在“刷新”我的页面,因此我用一些新的上下文数据回忆我的上一页,我返回self.render\u to\u响应(self.compute\u context(请求,用户名))但你并没有简单地用这种观点来解释你希望发生什么。你给它打电话,然后扔掉回答:正如我说的,你真的想退货吗?或者你想调用它的compute_context方法,用它返回的值做些什么?事实上,这一行完全没有意义。因为你调用它,然后对结果不做任何处理。我编辑了上面的代码并删除了compute_上下文,但它仍然不起作用。我是否应该从ShowAppSview继承ManageAppView以访问该方法?同上@Daniel Roseman如果compute\u context
是您想要返回的内容,您将需要它。它是非标准的,所以可能应该在get\u context
或类似的环境中。我并没有提供一个完整的修复程序,但提供了一个探索/调查的路径。我发现,如果您显示appsview.as_view()(request,*args,**kwargs),实际上可以通过ContextMixin将args和kwargs传递到get_context_数据方法,在这里它们显示为self.args和self.kwargs。这对于重写此方法和添加到上下文(例如表单)非常有用。我发现这在函数视图中也很有用。通过上面的代码,我可以从函数视图调用基于类的视图。不再工作了。。到目前为止,我看到的唯一选项是使用django.test.Client
,但在使用DRF时,它抛出的'request'参数必须是'django.http.HttpRequest'的实例,而不是'rest\u framework.request.request'。