【发布时间】:2020-02-20 02:35:23
【问题描述】:
Django 初学者。我有一个功能正常的 Django 应用程序(模型、视图、url、模板),类似于您在教程中看到的内容。我想在base.html 的类似终端的部分中返回一些调试和用户信息(例如,如果一个项目已经存在于表中)。
在我的标准视图函数调用中(将上传的 csv 文件转换为 pandas 并在我的规范化表中生成插入)我添加了第二个渲染(请求,...),它将字典发送到我的 base.html。
def bulkinsert(request):
data=None
context=None
if request.method== "POST" and request.FILES['myfile']:
myfile = request.FILES['myfile']
df=pd.read_csv(myfile )
data_html=df.to_html()
context={'data': data_html }
m=''
# now read each row and insert lot, sublot, ...
for index, row in df.iterrows():
if pd.notna(row['lot']):
lotname=row['lot']
...
# check existence of this lot
f1=Lot.objects.filter(lotname=lotname).count()
if f1==0:
m+='\n Lot did not exist yet, inserting ...'
b1=Lot(lotname=lotname)
b1.save()
else:
m+=f'\n Lot {lotname} already exists'
...
c2={ 'memo': m}
render(request, 'lenses/base.html',c2)
return render(request, 'bulkinsert.html',context)
else:
form=UploadFileForm()
data=None
context=None
return render(request, 'bulkinsert.html',context)
我必须添加什么到我的base.html 才能将该字典 (c2) 传递到所需的 html 部分?
到目前为止,我刚刚:
{% block c2 %}
<p>{{ memo }}</p>
{% endblock %}
但是页面没有像我预期的那样更新。
【问题讨论】: