ponder-lab / Imperative-DL-Study-Web-App

0 stars 0 forks source link

Query parameter problem with adding categorizations #76

Closed khatchad closed 3 years ago

khatchad commented 3 years ago

The URL that brings me to add a categorization, for example, is https://fathomless-inlet-57767.herokuapp.com/ponder/categorizations/add%3Fcommit=00d519bb222601f8a264c4abe311757e5079e03f. My understanding is that %3F is ?. However, if I replace %3F with ? in the URL, i.e., https://fathomless-inlet-57767.herokuapp.com/ponder/categorizations/add?commit=00d519bb222601f8a264c4abe311757e5079e03f, I get a MultiValueDictKeyError exception. My understanding is that the behavior should be the same (and also we should never have exceptions surface to the user):

Environment:

Request Method: GET
Request URL: https://fathomless-inlet-57767.herokuapp.com/ponder/categorizations/add?commit=00d519bb222601f8a264c4abe311757e5079e03f

Django Version: 3.1.7
Python Version: 3.7.8
Installed Applications:
['ponder.apps.BugFixesConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_tables2',
 'debug_toolbar',
 'crispy_forms',
 'django_filters',
 'bootstrap3',
 'widget_tweaks',
 'bootstrap_modal_forms',
 'rest_framework']
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',
 'debug_toolbar.middleware.DebugToolbarMiddleware']

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/datastructures.py", line 76, in __getitem__
    list_ = super().__getitem__(key)

During handling of the above exception ('user'), another exception occurred:
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/app/ponder/views.py", line 67, in search
    userID = request.GET['user']
  File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)

Exception Type: MultiValueDictKeyError at /ponder/categorizations/add
Exception Value: 'user'
khatchad commented 3 years ago

@mZneit I fixed this issue but in the process I broke the categorizations view. Do you know:

  1. What the search function is, and
  2. Why it is called when I go to https://fathomless-inlet-57767.herokuapp.com/ponder/categorizations/add?commit=00a33f7e060caa6616c15003bdac95b7926b76ae

It seems that this function is called any time a query parameter is being used. If that's the case, different views can use query parameters. We can't have it only work for a single view. The way I see it, we need to either:

  1. Figure out why this function is being called and see if it can be avoided by going directly to the desired view.
  2. Figure out how to make it work for any view.
khatchad commented 3 years ago

I had a look at the search() code I removed in more detail. It seems very tied to the categorizations view. The add route is not meant to be a view but rather an insertion form (the route should really be called new instead of add). Either way, I am having trouble understanding why this function is being called in this context.

mZneit commented 3 years ago

The search function filters the categorization based on the user ID. I should have called it filter_categorizations or a similar name.

khatchad commented 3 years ago

Is this something specific to our app or standard for Django?

khatchad commented 3 years ago

It's being called for the categorizations insertion route. Cannot insertion routes have query parameters? Or, is there something forcing routes with query parameters to go to this function?

mZneit commented 3 years ago

It's the view that directs to this page https://fathomless-inlet-57767.herokuapp.com/ponder/categorizations?user=14 and filters the categorizations. I'm not sure why it's showing up in the error message.

khatchad commented 3 years ago

Also, search what? Categorizations? Bug fixes?

mZneit commented 3 years ago

I meant by it to search for the Categorizations for a logged in user with a specific ID. But I know it should have a better name.

khatchad commented 3 years ago

Ok I see it in urls.py.

mZneit commented 3 years ago

the problem is with the path?

mZneit commented 3 years ago

I think the problem is the way I wrote the URL and rendered the page. It should be more robust.

khatchad commented 3 years ago

I reverted the changes in search(), which fixes the categorization view. I also moved up the route for add(), so that function doesn't get called. However, I am getting an error now with a missing pk variable.

mZneit commented 3 years ago

else: return HttpResponse('

Page Not Found

Categorizations cannot be found or viewed

', status=404)

is missing from the search method I believe.

khatchad commented 3 years ago

Fixed by https://github.com/ponder-lab/Imperative-DL-Study-Web-App/commit/5c66a3dcd9c63097f8e11cde1ea962eca6808b1d

khatchad commented 3 years ago

the problem is with the path?

It's fixed now. Thanks.

khatchad commented 3 years ago

I meant by it to search for the Categorizations for a logged in user with a specific ID. But I know it should have a better name.

Yes. Please see #80.

mZneit commented 3 years ago

Ok will work on that.