Results 1 to 5 of 5

Thread: Django credit card redaction app - - MultiValueDictKeyError

  1. #1
    Join Date
    May 2007
    Location
    West Indies
    Beans
    497
    Distro
    Ubuntu

    Django credit card redaction app - - MultiValueDictKeyError

    I’m trying to run a basic Django app which redacts a 16 digit number entered by the user. I had a it running a few minutes ago. I’m not sure what I changed, but now I am getting a MultiValueDictKeyError. I’ve triple checked every variable. The only dictionary in my project is in views.py at line 7. As far as I can tell, it’s all accurate and positioned correctly. I don’t recall changing this dictionary or any other operator around this line. I’m stumped. What would you people suggest? Have a look at my setup.

    Here is the error and traceback in full: https://pastebin.com/QwSrmAJx

    Here is my urls.py:
    Code:
    from django.conf.urls import  include, url
    from django.contrib import admin
    from django.urls import path
    from . import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^$', views.home, name='home'),
    ]
    views.py:
    Code:
    from django.http import HttpResponse
    from django.shortcuts import render
    
    def home(request):
        number = request.GET['ccEntry']
        redacted_num = 'xxxx xxxx xxxx {}'.format(number[-4:])
        return render(request, 'home.html', {'number':number, 'redacted_num':redacted_num})
    Template (home.html): https://pastebin.com/S2qxzRPG

    (I would have included the contents of my home.html template but vBulletin gave me a 403 error.)

    Here is the traceback:
    Code:
    Here is the error, traceback and Request information:
    
    MultiValueDictKeyError at /
    'ccEntry'
    Request Method:
    GET
    Request URL:
    http://127.0.0.1:8000/
    Django Version:
    2.0.2
    Exception Type:
    MultiValueDictKeyError
    Exception Value:
    'ccEntry'
    Exception Location:
    /home/<user>/.local/lib/python3.7/site-packages/django/utils/datastructures.py in __getitem__, line 79
    Python Executable:
    /usr/sbin/python
    Python Version:
    3.7.2
    Python Path:
    ['/home/<user>/dev/projects/python/2018-and-2019/cel2fah-original_with_CC-redact-project-_Django202/first_project_attempt',
     '/usr/lib/python37.zip',
     '/usr/lib/python3.7',
     '/usr/lib/python3.7/lib-dynload',
     '/home/<user>/.local/lib/python3.7/site-packages',
     '/usr/lib/python3.7/site-packages',
     '/usr/lib/python3.7/site-packages/setuptools-40.6.2-py3.7.egg']
    Server time:
    
    Thu, 7 Mar 2019 17:39:40 +0000
    
    Environment:
    Request Method: GET
    Request URL: http://127.0.0.1:8000/
    
    Django Version: 2.0.2
    Python Version: 3.7.2
    Installed Applications:
    ['django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles']
    Installed Middleware:
    ['django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware']
    
    Traceback:
    File "/home/<user>/.local/lib/python3.7/site-packages/django/utils/datastructures.py" in __getitem__
      77.             list_ = super().__getitem__(key)
    
    During handling of the above exception ('ccEntry'), another exception occurred:
    
    File "/home/<user>/.local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
      35.             response = get_response(request)
    
    File "/home/<user>/.local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
      128.                 response = self.process_exception_by_middleware(e, request)
    
    File "/home/<user>/.local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
      126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
    
    File "/home/<user>/dev/projects/python/2018-and-2019/cel2fah-original_with_CC-redact-project-_Django202/first_project_attempt/first_project_attempt/views.py" in home
      5.     number = request.GET['ccEntry']
    
    File "/home/<user>/.local/lib/python3.7/site-packages/django/utils/datastructures.py" in __getitem__
      79.             raise MultiValueDictKeyError(key)
    
    Exception Type: MultiValueDictKeyError at /
    Exception Value: 'ccEntry'
    
    Request information
    USER
    AnonymousUser
    GET
    No GET data
    POST
    No POST data
    FILES
    No FILES data
    I’ve double checked to make sure that I include my data in my form element as you can see in the above template. The data is present as “ccEntry” in both home.html and in views.py.

    In my home.html template, even though the instructor instructor in my Udemy course doesn’t use it, I added a method="get" attribute to my HTML form tag. This didn’t change anything.

    I also looked up “Django request.GET”. Based on that SO answer, In my views.py I changed the line (where I declare the number variable) from number = request.GET['ccEntry'] to number = request.GET.get['ccEntry', None]. That gives a different error (a type error this time): https://pastebin.com/L81LVtzi


    This is a long shot, but I thought I would share a link to my source code hosted on GitHub with a requirements.txt included. If any of you would like to test this out yourself, I am accepting pull requests. Here it is: https://github.com/Angeles4four/CC_Redact
    My rig:
    IBM Personal System/2 Model 30-286 - - Intel 80286 (16 bit) 10 Mhz - - 1MB DRAM - - Integrated VGA Display adapter
    1.44MB capacity Floppy Disk - - PS/2 keyboard (no mouse)

  2. #2
    Join Date
    Jan 2017
    Beans
    235

    Re: Django credit card redaction app - - MultiValueDictKeyError

    I played around with your app and got it work with the following hack.

    views.py:
    Code:
    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')

    It seems that something in your code is triggering an additional 'GET' request. I don't know enough about django to offer an explanation for this behavior. I turned on logging and took a look at the output from curl as suggested in this post (https://groups.google.com/forum/#!ms...0/KEIkguUcqxYJ) but did not spot anything.


    HTH

  3. #3
    Join Date
    May 2007
    Location
    West Indies
    Beans
    497
    Distro
    Ubuntu

    Re: Django credit card redaction app - - MultiValueDictKeyError

    Howdy @norobro!

    Eureka! This hack works! Thank you very much for taking the time to play around with my project to find a solution. I committed and pushed my changes up to GitHub.

    I know you said that you’re not an expert in Django. That's OK. I will ask on Django forums elsewhere about why this condition is required for Django to execute views.py properly and then I’ll be sure to report back here with the explanation.

    Thanks again, my friend!
    Last edited by Drone4four; March 14th, 2019 at 02:27 PM. Reason: grammar
    My rig:
    IBM Personal System/2 Model 30-286 - - Intel 80286 (16 bit) 10 Mhz - - 1MB DRAM - - Integrated VGA Display adapter
    1.44MB capacity Floppy Disk - - PS/2 keyboard (no mouse)

  4. #4
    Join Date
    May 2007
    Location
    West Indies
    Beans
    497
    Distro
    Ubuntu

    Re: Django credit card redaction app - - MultiValueDictKeyError

    Here’s the explanation for why this hack works the way it does. It’s simple. If a user lands on the webpage and the ccEntry parameter is not present, it throws MultiValueDictKeyError. So the conditional checks for the presence of the parameter.

    The forum member over at Django user Google group clarifies in greater detail:

    When a user requests your home page's URL the first time in order to load your home.html file, the GET request they send to your server does not contain the 'ccEntry' parameter, which raised the MultiValueDictKeyError whenever your view was executing. However, in your edited view, you take care of that by first checking whether the GET request contains 'ccEntry', which is why it is working correctly now.
    My rig:
    IBM Personal System/2 Model 30-286 - - Intel 80286 (16 bit) 10 Mhz - - 1MB DRAM - - Integrated VGA Display adapter
    1.44MB capacity Floppy Disk - - PS/2 keyboard (no mouse)

  5. #5
    Join Date
    Jan 2017
    Beans
    235

    Re: Django credit card redaction app - - MultiValueDictKeyError

    Perusing the Mozilla Django tutorial I found this forms request flow chart: https://developer.mozilla.org/en-US/...ndling_process

    Apparently the initial request is an empty "GET" request, i.e. no variables, which triggers your error.

    Perhaps a more Djangoic (Pythonic) check would be to test if the dictionary HttpRequest.GET or HttpRequest.content_params contain data (https://docs.djangoproject.com/en/2....content_params):
    Code:
    def home(request):    
        if 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')
    Last edited by norobro; March 15th, 2019 at 03:18 AM. Reason: added HttpRequest.GET

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •