如何发送字典上下文数据以从 django 中的模板查看
How to send dictionary context data to view from template in django
我正在将一个字典对象传递给模板并使用该字典对象来填充 table 数据。但是根据需要,我想将该字典数据发送到另一个视图进行处理。我尝试使用影响字典中数据的 URL 参数发送数据。
查看
class GeneratedReportView(View):
"""
View to display all details about a single patient
@param:
is_single_document: bool, if true it means only single document exist for patient
and to display only one col in html page
"""
model = Patient
template_name = 'patient/patient_generated_report.html'
context_object_name = 'object'
form = GeneratedReportForm()
# helper function
def create_final_report(self, doc1_obj: Document, doc2_obj: Document = None, is_single_document: bool = False) :
# template of how data is passed on to html page
table = {
'doc1_name':'',
'doc2_name':'',
'label':{
'doc1': {'value':'value', 'unit':'unit'},
'doc2': {'value':'value', 'unit':'unit'},
'remark': '', #`doc1_value - doc2_value`,
'remark_color': '', #`red or green`,
}
}
#
# some code to populate table data
#
return table, is_single_document
def get(self, request, pk):
# instantiating form for selected particular user
form = GeneratedReportForm(pk=pk)
patient_obj = Patient.objects.get(pk=pk)
# retrieving all document of patient.pk = pk from latest to oldest
documents = Document.objects.filter(patient__id=pk).order_by('-uploaded_at')
table, is_single_document = None, None
doc1_obj, doc2_obj = None, None
try:
doc2_obj = documents[0] # most recent report
doc1_obj = documents[1] # second most recent report
except Exception as e:
print('ERROR while getting doc2, doc2 obj', e)
if doc2_obj is not None:
table, is_single_document = self.create_final_report(doc2_obj, doc1_obj)
if table is not None:
table = dict(table) # <-- NOTICE HERE
context = {
'table':table, # <-- NOTICE HERE, table variable is dict type
'patient_obj':patient_obj,
'doc1_obj':doc1_obj,
'doc2_obj':doc2_obj,
'is_single_document' : is_single_document,
'form':form ,
}
return render(request, self.template_name, context)
模板
<table id="example" class="table table-striped">
<thead>
<tr>
<th>Label</th>
{% if not is_single_document %}
{% if doc1_obj %} <th>{{doc1_obj}}</th> {% else %} <th>Old Report</th> {% endif %}
{% endif %}
{% if doc2_obj %}<th>{{doc2_obj}}</th> {% else %} <th>Latest Report</th> {% endif %}
<th>remark</th>
</tr>
</thead>
<tbody>
{% if table %}
{% for label, docs in table.items %}
<tr>
<td>{{label}}</td>
<td>{{docs.doc1.value}}</td>
{% if not is_single_document %}
<td> {{docs.doc2.value}}</td>
{% endif %}
<td {% if not is_single_document %}bgcolor="{{docs.remark_color}}" {% endif %}>{{docs.remark}}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
<a href='{% url 'save' table %}'>
<button class="GFG">
Approve now
</button>
</a>
我正在尝试使用此 URL conf 将 table
发送到另一个视图,但在视图中获得的数据不正确,包含 %
和其他字符。
url(r'^save/(?P<oid>.*)$', GeneratedReportSaveView.as_view(), name='save'),
我会很感激一些推荐和建议。谢谢
多方搜索后,在Django-forum上找到一个引导我制定解决方案的建议,讨论可见here。
我通过重新定义问题 如何在视图之间传递数据来找到解决方案? 这可以通过使用 Django-session
来完成。基本上它是关于在 request.session
中保存数据,只要会话处于活动状态,就可以在任何视图中轻松访问这些数据。
class GeneratedReportView(View):
def create_final_report(self, doc1_obj: Document, doc2_obj: Document = None, is_single_document: bool = False) :
#
# ...No change here...
#
return table, is_single_document
def get(self, request, pk):
# normal code ..
request.session['table'] = table # <--- NOTICE HERE
context = {
'table':table,
'patient_obj':patient_obj,
'doc1_obj':doc1_obj,
'doc2_obj':doc2_obj,
'is_single_document' : is_single_document,
'form':form ,
}
return render(request, self.template_name, context)
现在只要不删除或关闭会话,就可以在任何其他视图中访问此数据。
class AnotherView(View):
def get(self, request):
table = request.session['table'] # <- Getting table data from another view
# further code
我正在将一个字典对象传递给模板并使用该字典对象来填充 table 数据。但是根据需要,我想将该字典数据发送到另一个视图进行处理。我尝试使用影响字典中数据的 URL 参数发送数据。
查看
class GeneratedReportView(View):
"""
View to display all details about a single patient
@param:
is_single_document: bool, if true it means only single document exist for patient
and to display only one col in html page
"""
model = Patient
template_name = 'patient/patient_generated_report.html'
context_object_name = 'object'
form = GeneratedReportForm()
# helper function
def create_final_report(self, doc1_obj: Document, doc2_obj: Document = None, is_single_document: bool = False) :
# template of how data is passed on to html page
table = {
'doc1_name':'',
'doc2_name':'',
'label':{
'doc1': {'value':'value', 'unit':'unit'},
'doc2': {'value':'value', 'unit':'unit'},
'remark': '', #`doc1_value - doc2_value`,
'remark_color': '', #`red or green`,
}
}
#
# some code to populate table data
#
return table, is_single_document
def get(self, request, pk):
# instantiating form for selected particular user
form = GeneratedReportForm(pk=pk)
patient_obj = Patient.objects.get(pk=pk)
# retrieving all document of patient.pk = pk from latest to oldest
documents = Document.objects.filter(patient__id=pk).order_by('-uploaded_at')
table, is_single_document = None, None
doc1_obj, doc2_obj = None, None
try:
doc2_obj = documents[0] # most recent report
doc1_obj = documents[1] # second most recent report
except Exception as e:
print('ERROR while getting doc2, doc2 obj', e)
if doc2_obj is not None:
table, is_single_document = self.create_final_report(doc2_obj, doc1_obj)
if table is not None:
table = dict(table) # <-- NOTICE HERE
context = {
'table':table, # <-- NOTICE HERE, table variable is dict type
'patient_obj':patient_obj,
'doc1_obj':doc1_obj,
'doc2_obj':doc2_obj,
'is_single_document' : is_single_document,
'form':form ,
}
return render(request, self.template_name, context)
模板
<table id="example" class="table table-striped">
<thead>
<tr>
<th>Label</th>
{% if not is_single_document %}
{% if doc1_obj %} <th>{{doc1_obj}}</th> {% else %} <th>Old Report</th> {% endif %}
{% endif %}
{% if doc2_obj %}<th>{{doc2_obj}}</th> {% else %} <th>Latest Report</th> {% endif %}
<th>remark</th>
</tr>
</thead>
<tbody>
{% if table %}
{% for label, docs in table.items %}
<tr>
<td>{{label}}</td>
<td>{{docs.doc1.value}}</td>
{% if not is_single_document %}
<td> {{docs.doc2.value}}</td>
{% endif %}
<td {% if not is_single_document %}bgcolor="{{docs.remark_color}}" {% endif %}>{{docs.remark}}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
<a href='{% url 'save' table %}'>
<button class="GFG">
Approve now
</button>
</a>
我正在尝试使用此 URL conf 将 table
发送到另一个视图,但在视图中获得的数据不正确,包含 %
和其他字符。
url(r'^save/(?P<oid>.*)$', GeneratedReportSaveView.as_view(), name='save'),
我会很感激一些推荐和建议。谢谢
多方搜索后,在Django-forum上找到一个引导我制定解决方案的建议,讨论可见here。
我通过重新定义问题 如何在视图之间传递数据来找到解决方案? 这可以通过使用 Django-session
来完成。基本上它是关于在 request.session
中保存数据,只要会话处于活动状态,就可以在任何视图中轻松访问这些数据。
class GeneratedReportView(View):
def create_final_report(self, doc1_obj: Document, doc2_obj: Document = None, is_single_document: bool = False) :
#
# ...No change here...
#
return table, is_single_document
def get(self, request, pk):
# normal code ..
request.session['table'] = table # <--- NOTICE HERE
context = {
'table':table,
'patient_obj':patient_obj,
'doc1_obj':doc1_obj,
'doc2_obj':doc2_obj,
'is_single_document' : is_single_document,
'form':form ,
}
return render(request, self.template_name, context)
现在只要不删除或关闭会话,就可以在任何其他视图中访问此数据。
class AnotherView(View):
def get(self, request):
table = request.session['table'] # <- Getting table data from another view
# further code