Solution 1 :

yes, I had a views.py file like this:

from .models import Product
ac_products = Product.objects.filter(is_active=True)
def product_list(request):
    pros = ac_products
    return render(request, 'pro.html', {"pros": pros})

but this function had a problem, if a new object has been added/removed to/from the list of available products. this function didn’t consider the changes.
but I changed the code to this below one and then it works fine:

from .models import Product
def product_list(request):
    pros = Product.objects.filter(is_active=True)
    return render(request, 'pro.html', {"pros": pros})

maybe this could help.

  • I did the first kind of declaration because I had the same filtering on Product model for a few more functions too and I thought it’s better to define the filtering just once but it wasn’t

Problem :

Im using python 3.8.1 and django 3.0.3
Using pymysql for mysql connection

I have two users – (admin and worker)

When worker add (food) in foods table, the food list template shows the newly added item (for worker).

But at the same time in another browser, if admin refresh the food list page, the new value is not displayed until server restart .

Is there any fix for it.

Im not using any models.
Thank you

Views.py of worker

from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
#import MySQLdb
import pymysql
import simplejson as json
import random
import urllib.request
import webbrowser
from datetime import date
conn=pymysql.connect("localhost","root","","babycare")
c=conn.cursor()



def addfood(request):
if request.POST:
    ashaworker_id=request.session['userid']
    get_panchayah="select district,panchayath,ward_no from worker_reg where 
    wrkr_id='"+str(ashaworker_id)+"'"
    c.execute(get_panchayah)
    wrkr_details=c.fetchone()

    ftitle=request.POST.get("ftitle")
    fdetails=request.POST.get("fdetails")
    posted_date=date.today()


    food_exists="select count(*) from food where 
    wrkr_id='"+str(ashaworker_id)+"' and title='"+str(ftitle)+"' and 
    posted_date='"+str(posted_date)+"' and status='1' "
    print("-------"+food_exists+"-------")
    c.execute(food_exists)
    exisist_data=c.fetchone()
    # try:
    if exisist_data[0]>0:
            message="Such details already exisist"
            return render(request,"Add_food.html",{"message":message})
    else:
            food_insert="insert into 






   food(`wrkr_id`,`title`,`details`,`posted_date`,`status`)values
    ('"+str(ashaworker_ 

    id)+"','"+str(ftitle)+"','"+str(fdetails)+"','"+str(posted_date)+"','1')"
            c.execute(food_insert)
            conn.commit()
            msg="The ashaworker added something new food details please check 
            it on, don't be late posted on "+str(posted_date)
            food_sms="select phone_number from mother_reg where 
            wrker_id='"+str(ashaworker_id)+"'"
            c.execute(food_sms)
            phone_data=c.fetchall()
            for p in phone_data:
                sendsms(p[0],msg)

            message="Added Successfully"
            return render(request,"Add_food.html",{"message":message})
    # except:
    #     message="Such details already exisist"
    #     return render(request,"Add_mother.html",{"message":message}) 
return render(request,'Add_food.html')




def viewfood(request):
ashaworker_id=request.session['userid']
get_panchayah="select district,panchayath,ward_no from worker_reg where 
wrkr_id='"+str(ashaworker_id)+"'"
c.execute(get_panchayah)
wrkr_details=c.fetchone()

view_food="select * from food where wrkr_id='"+str(ashaworker_id)+"'"
c.execute(view_food)
view_data=c.fetchall()
return render(request,'viewfood.html',{"data":view_data})

Views.py file for admin

from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
#import MySQLdb
import pymysql
import simplejson as json
import random
import urllib.request
import webbrowser
from datetime import date


conn=pymysql.connect("localhost","root","","babycare")
c=conn.cursor()
def view_food(request):
view_food="SELECT 
f.food_id,w.`worker_name`,w.`phone_no`,w.`panchayath`,w.`district`,f.`title`
,f.`d etails`,f.`posted_date` FROM `worker_reg` w,`food` f WHERE 
f.`wrkr_id`=w.`wrkr_id`"
c.execute(view_food)
view_data=c.fetchall()
return render(request,'View_food.html',{"data":view_data})

Template file for viewing food table

        {%for d in data%}
        <tr>
        <td class="tcell" style="border-radius: 5px 0px 0px 0px"><a 
        href="#">{{d.5}}</a> </td>
            <td class="tcell">{{d.0}}</td>
            <td class="tcell">{{d.1}}</td>
            <td class="tcell">{{d.2}}</td>
            <td class="tcell">{{d.3}}</td>
            <td class="tcell">{{d.4}}</td>

            <td class="tcell">{{d.6}}</td>
            <td class="tcell">{{d.7}}</td>

        </tr>
        {% endfor %}

urls.py code

from django.contrib import admin
from django.urls import path
from.import CommonView
from government import govviews
from ashaworker import ashaviews
from Mother import motherviews
from Doctor import docviews

urlpatterns = [
path('admin/', admin.site.urls),
path('',CommonView.index),
path('login/',CommonView.login),
#--------Government---------------
path('adminhome/',govviews.adminhome),
path('panchayathlink/',govviews.panchayathlink),
path('panchayathreg/',govviews.panchayathreg),
path('viewpanchayath/',govviews.viewpanchayath),
path('workerlink/',govviews.workerlink),
path('workerreg/',govviews.workerreg),
path('viewworker/',govviews.viewworker),
path('panchayathlistview/',govviews.panchayathlistview),
path('projectlink/',govviews.projectlink),
path('addproject/',govviews.addproject),
path('viewprojects/',govviews.viewprojects),
path('doctorlink/',govviews.doctorlink),
path('adddoctor/',govviews.adddoctor),
path('viewdoctor/',govviews.viewdoctor),
path('allviews/',govviews.allviews),
path('view_vacc/',govviews.view_vacc),
path('view_food/',govviews.view_food),
path('view_disease/',govviews.view_disease),
path('view_health/',govviews.view_health),
path('deletepanchayath/',govviews.deletepanchayath),
path('deleteworker/',govviews.deleteworker),
path('deleteproject/',govviews.deleteproject),
path('deletedoctor/',govviews.deletedoctor),
path('admin_logout/',govviews.logout),

 #--------Ashaworker---------------
path('workerhome/',ashaviews.workerhome),
path('govnotification/',ashaviews.govnotification),
path('motherlink/',ashaviews.motherlink),
path('addmother/',ashaviews.addmother),
path('viewmother/',ashaviews.viewmother),
path('alertlink/',ashaviews.alertlink),
path('addvaccination/',ashaviews.addvaccination),
path('viewvaccination/',ashaviews.viewvaccination),
path('addfood/',ashaviews.addfood),
path('viewfood/',ashaviews.viewfood),
path('healthlink/',ashaviews.healthlink),
path('addhealthtips/',ashaviews.addhealthtips),
path('viewhealthtips/',ashaviews.viewhealthtips),
path('adddisease/',ashaviews.adddisease),
path('viewdisease/',ashaviews.viewdisease),
path('deletemother/',ashaviews.deletemother),
path('delete_vacc/',ashaviews.delete_vacc),
path('delete_food/',ashaviews.delete_food),
path('delete_tips/',ashaviews.delete_tips),
path('delete_disease/',ashaviews.delete_disease),
path('worker_logout/',ashaviews.logout),
path('worker_profile/',ashaviews.worker_profile),
path('worker_view_doctor/',ashaviews.worker_view_doctor),

#--------Mother---------------
path('motherhome/',motherviews.motherhome),
path('vaccination_alerts/',motherviews.vaccination_alerts),
path('food_details/',motherviews.food_details),
path('health_tips/',motherviews.health_tips),
path('disease_details/',motherviews.disease_details),
path('mother_profile/',motherviews.mother_profile),
path('mother_view_doctor/',motherviews.mother_view_doctor),
path('mother_view_worker/',motherviews.mother_view_worker),
path('mother_logout/',motherviews.logout),

#--------Doctor---------------
path('doctorhome/',docviews.doctorhome),
path('mothers/',docviews.viewmothers),
path('workers/',docviews.viewworkers),
path('doctor_logout/',docviews.logout),


]

worker adding food

food not showing in admin page unless restert server

Comments

Comment posted by mh-firouzjah

hi, I had this problem too, and solved it by calling the querysets inside the views.py functions instead of global block. that works fine but I don’t know if it is a good and practical solution or not.

Comment posted by user13003297

Him can you please show how to implement queryset. Thanks

Comment posted by mh-firouzjah

but I want to say, this is django, why you don’t use ORM?

Comment posted by mh-firouzjah

yes, thats because according to djnago docs, the querysets are lazy, and they would be catched untill it is forced to re-catch. read the docs about queryset.

Comment posted by mh-firouzjah

defining a global variable to hold the queryset will make it catched and then you didn’t call for any renew/refresh to get the new queryset, so the lazy lady won’t do that.

By