Django function view to update two templates (current and base)

Viewed 78

Django Beginner. I have a functioning Django app (models, views, urls, templates) similar to what you can see in tutorials. I would like to return some debugging and user information (e.g. if an item already exists in a table) into a terminal-like section in my base.html.

Inside my standard view function call (turning an uploaded csv file to pandas and generating inserts into my normalized tables) I added a 2nd render(request,...) that sends a dictionary to my 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)   

What do I have to add to my base.html to pass that dictionary (c2) into the desired html-section?

So far I have just:

{% block c2 %}      
<p>{{ memo }}</p>         
{% endblock %}

but the page does not update as I expected.

1 Answers

So, I dont have enough reputation to comment. So I did some assumptions, and have some remarks.

Assumptions: What I assume you want to do is process the file that got uploaded, and return a message commenting on every line whether it already existed or got created. You want to return 1 page containing all that content, so it's not dynamic..

Remarks: I don't understand why you are calling the render function, but are not returning it. Furthermore you are overwriting your c2 with every iteration.

Based on what I assume what you want to do I suggest the following code:

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)   

Now you will need to itterate over your memos. So also adjust your base.html

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