【问题标题】:Django function view to update two templates (current and base)Django 函数视图更新两个模板(当前和基础)
【发布时间】: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 %}

但是页面没有像我预期的那样更新。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    所以,我没有足够的声誉来发表评论。所以我做了一些假设,并发表了一些评论。

    假设: 我假设您想要做的是处理上传的文件,并返回一条消息,评论每一行是否已经存在或已创建。您想返回包含所有内容的 1 页,因此它不是动态的..

    备注:我不明白你为什么调用渲染函数,但没有返回它。此外,您每次迭代都会覆盖您的 c2 。

    根据我假设你想要做什么,我建议以下代码:

    views.py

    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 }   
            memos= []
    
            # 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:
                        message = '\n Lot did not exist yet, inserting ...'
                        b1=Lot(lotname=lotname)
                        b1.save() 
                    else: 
                        message = f'\n Lot {lotname} already exists'
     ...            memos.append(message)
            context['memos'] = memos
            return render(request, 'lenses/base.html',context)
        else: 
            form=UploadFileForm()
            data=None
            context=None
        return render(request, 'bulkinsert.html',context)   
    

    现在您需要遍历您的备忘录。所以也要调整你的base.html

    {% block c2 %}
    {% for memo in memos %}      
        <p>{{ memo }}</p>
    {% endfor %}         
    {% endblock %}
    

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2019-08-11
      • 2013-01-21
      • 2014-06-09
      • 2013-06-16
      • 2019-06-20
      • 2014-02-01
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多