Python Forum
Django credit card redaction app - - MultiValueDictKeyError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django credit card redaction app - - MultiValueDictKeyError
#2
I’ve discovered a ‘hack’ to correct the issue. I committed and pushed my changes up to my GitHub repo for those curious enough to try it out yourself.

My problem now is that I don’t really understand why it works. Here I will explain what I do know and then my ask would be for you people to fill in the gaps in my explanation. First I share my working code.

Here is my views.py which includes the relevant function I use:

from django.http import HttpResponse
from django.shortcuts import render

def home(request):
   if 'ccEntry' in request.GET:
       number = request.GET['ccEntry']
       redacted_num = 'xxxx xxxx xxxx {}'.format(number[-4:])
       return render(request, 'home.html', {'number':number, 'redacted_num':redacted_num})
   else:
       return render(request, 'home.html')
Here is my lucid, cerebral explanation of the above Python code in plain English: At line 4 I am defining the home function. Then there is a conditional referring to the ‘ccEntry’ string inside the template. If ccEntry is present in the GET request, then a number variable is declared based on the user input on the webpage. Then a redacted_num variable is declared which will appear as a string (‘xxxx xxxx xxxx ’ with the last 4 characters lopped off (the slice)). If all of the above is in order, then the render function will be returned:
  • with the standard request,
  • with reference to the home.html template,
  • along with a dictionary with a ‘number’ string matching up with the number variable. Ditto for the redact_num.
However if the above condition is false, then the render function will return the standard request and home.html template without a dictionary. That’s pretty much everything I understand.

I’m not sure why a conditional as it appears above is necessary.

Would someone here care to explain, if you can?
Reply


Messages In This Thread
RE: Django credit card redaction app - - MultiValueDictKeyError - by Drone4four - Mar-14-2019, 02:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,411 Jun-30-2019, 12:21 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020