I want to pass a context value without rendering a page, that’s why I implemented context processor. But when I return a context value from that function it showing me this error. here is my code for payment.py:
def payment_url(request): todays_date = date.today() now = datetime.datetime.now() mcnt_TxnNo = "Txn" + str(todays_date.year) + str('{:02d}'.format( todays_date.month)) + str(todays_date.day) + str(now.hour) + str(now.minute) + str('{:02d}'.format(now.second)) order_no = "ON" + str(todays_date.year) + str('{:02d}'.format( todays_date.month)) + str(todays_date.day) + str(now.hour) + str(now.minute) + str('{:02d}'.format(now.second)) secret_key = b"***" print(mcnt_TxnNo) if request.method == 'POST': name = request.POST["clientName"] phone = request.POST["phone"] address = request.POST["address"] email = request.POST["clientEmail"] product_name = request.POST["proName"] print("product Name", product_name) paid = request.POST["paid"] print(name) string = "***" print(string) byte_string = bytes(string, 'utf-8') hash_code = hmac.new(secret_key.upper(), byte_string, hashlib.sha256) hash_text = hash_code.hexdigest() print(hash_text) jsonObj = { // json object } json_string = json.dumps(jsonObj) print(json_string) url = "**" headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} r = requests.post(url, data=json_string, headers=headers) print(r.json()) j = r.json()['status'] k = r.json()['data'] if j == "200": payment_id = k['payment_id'] redirect_url = k['redirect_url'] payment_link = str(redirect_url) + "?" + str(payment_id) print(payment_link) return {'payment_link': payment_link} else: return {}
And my setting.py:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'users.payment.payment_url', ], }, }, ]
so, basically, I’m working on a project where I created a JSON request and generated a payment_url so I returned it so that I can access it from any page. I also returned an empty dictionary when payment_url is none, But still getting the same error.
Traceback:
File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangocorehandlersexception.py", line 34, in inner response = get_response(request) File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangocorehandlersbase.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangocorehandlersbase.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:UsersuserDesktopDjango Web AppLen-denlen_denpagesviews.py", line 25, in index return render(request, 'pages/index.html') File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangoshortcuts.py", line 36, in render content = loader.render_to_string(template_name, context, request, using=using) File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangotemplateloader.py", line 62, in render_to_string return template.render(context, request) File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangotemplatebackendsdjango.py", line 61, in render return self.template.render(context) File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangotemplatebase.py", line 169, in render with context.bind_template(self): File "C:UsersuserAppDataLocalProgramsPythonPython38libcontextlib.py", line 113, in __enter__ return next(self.gen) File "C:UsersuserDesktopDjango Web AppLen-denvenvlibsite-packagesdjangotemplatecontext.py", line 246, in bind_template updates.update(processor(self.request)) TypeError: 'NoneType' object is not iterable
Answer
return empty dict {} when method is GET.