代码之家  ›  专栏  ›  技术社区  ›  user7104029

在django中从单个视图渲染多个模板

  •  1
  • user7104029  · 技术社区  · 8 年前

    我想将上下文数据发送到一个html,并想呈现不同的html。 登录后,用户被重定向到此仪表板视图。这里我想呈现两个html文件,上下文值将被发送到一个html,比如temp1。html文件,但用户可以看到temp2。html文件。在temp2中。html和其他html文件,我将包括temp1。html文件。有什么办法吗? 视图。py公司

    def dashboard(request):
        print('in dashboard view')
        object = UserSelection.objects.get(user=request.user)
    
        if object.user_type == 'candidate':
            val_cand = CandidateDetail.objects.filter(candidate_username=request.user)
            if val_cand:
                print('Candidate filled data') #Already filled data
                data = CandidateDetail.objects.get(candidate_username=request.user)
                return render(request, 'dashboard.html',{'obj':object.user_type, 'data':data})
            else:
                print('new user') #Registered but not filled data
                return render(request, 'dashboard.html', {'obj':object.user_type})
    
        else:
            val_emp = EmployerDetail.objects.filter(name=request.user)
            if val_emp:
                print('Employer filled data') #Already filled data
                data = EmployerDetail.objects.get(name=request.user)
                return render(request, 'dashboard.html',{'obj':object.user_type, 'data':data})
            else:
                print('new user') #Registered but not filled data
                return render(request, 'dashboard.html', {'obj':object.user_type})
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   ramganesh    8 年前

    不能在单个视图中呈现两个html文件。请使用Django模板语言实现所需的行为。

    i、 e)如果您通过 obj dashboard.html 并且内部html文件也可以访问obj。

    仪表板html

    {{ obj }}
    
    {% include 'test1.html' %}
    {% include 'test2.html' %}
    

    可以使用关键字参数向模板传递其他上下文:

    {% include 'test1.html' with obj=obj additional_context='blah' %}
    

    测试1。html

    {{ obj }}