Skip to content
Advertisement

TypeError: not enough arguments for format string in python

It shows an error like TypeError: not enough arguments for format string, how can solve this issue.

views.py

def GetMobilefollowpopularnewsproviderTest(request,user_id):
# from django.http import JsonResponse
print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
if request.method == 'GET':
    # news_provider = request.POST.get("news_provider")
    print user_id
    cursor = connection.cursor()
    queryset = cursor.execute("select news_crawl_newsproviders.news_provider as id, 1 as status from news_crawl_newsproviders join  accounts_follownewsprovider on news_crawl_newsproviders.id=accounts_follownewsprovider.provider_id where accounts_follownewsprovider.user_id='%s' union select news_crawl_newsproviders.news_provider, 0 from news_crawl_newsproviders join  accounts_follownewsprovider on news_crawl_newsproviders.id=accounts_follownewsprovider.provider_id where accounts_follownewsprovider.user_id!='%s' and news_crawl_newsproviders.news_provider not in(select news_crawl_newsproviders.news_provider as id from news_crawl_newsproviders join  accounts_follownewsprovider on news_crawl_newsproviders.id=accounts_follownewsprovider.provider_id where accounts_follownewsprovider.user_id='%s');select news_crawl_newsproviders.news_provider as id, 1 as status from news_crawl_newsproviders join  accounts_follownewsprovider on news_crawl_newsproviders.id=accounts_follownewsprovider.provider_id where accounts_follownewsprovider.user_id='%s' union select news_crawl_newsproviders.news_provider, 0 from news_crawl_newsproviders join  accounts_follownewsprovider on news_crawl_newsproviders.id=accounts_follownewsprovider.provider_id where accounts_follownewsprovider.user_id!='%s' and news_crawl_newsproviders.news_provider not in(select news_crawl_newsproviders.news_provider as id from news_crawl_newsproviders join  accounts_follownewsprovider on news_crawl_newsproviders.id=accounts_follownewsprovider.provider_id where accounts_follownewsprovider.user_id='%s');"%(user_id))
    dict = {}
    dict = dictfetchall(cursor)
    print(dict)
    context = {
        'posts': dict
    }
    return JsonResponse(context, safe=False)
return HttpResponse(status=201)

urls.py

url(r'followpopularnewsprovider/(?P<user_id>d+)/$', 
 csrf_exempt(views.GetMobilefollowpopularnewsproviderTest), name='popularprovider')

Advertisement

Answer

In your queryset you have more than one %s although you pass %(user_id) one time

you should pass all parameter in format string you defined

cursor.execute("..." % (param1, param2, param3, param4,...))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement