我想将上下文数据发送到一个html,并希望呈现不同的html。 登录后,用户被重定向到此仪表板视图。这里我要渲染两个html文件,将上下文值发送到一个html让我们说temp1.html文件,但是用户可以看到temp2.html文件。在temp2.html和其他html文件中,我将包含temp1.html文件。有没有办法这样做? views.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})
答案 0 :(得分:0)
您无法在单个视图中呈现两个html文件。请使用Django模板语言获得所需的行为。
即如果你传递obj
到dashboard.html
和html内部文件也可以访问obj。
dashboard.html
{{ obj }}
{% include 'test1.html' %}
{% include 'test2.html' %}
您可以使用关键字参数将其他上下文传递给模板:
{% include 'test1.html' with obj=obj additional_context='blah' %}
test1.html
{{ obj }}