I'm looking for a way to redirect to a different page depending upon which submit button the user clicks. I included a formaction tag in the template, but django still demands that get_absolute_url be defined. Using Django 2.0
Basically, I'm trying to write a small app that launches a test case, which would allow a non-programmer to evaluate a small section of code responsible for a background process. The app should allow the user to add as many test items to it as they wish, one at a time. When an item is added, they can choose to click and 'evaluate' button or an 'add more items' button, and each button should go to a different page--basically, 'add more items' should refresh the same page. Clicking on 'add more items' does indeed post the data to the database, but it ignores the formaction tag. The stack trace says that form_valid is calling get_absolute_url on the model (which I have not defined), but the target url has to do with what the user wants to do next with said model, not the model itself. I haven't figured out how to override the get_absolute_url call based on which html button was clicked. (Though it also said I can provide an url, which I did on the template...)
(The code I'm pasting below may be a little awkward because I've been finding workarounds for things by trial and error; so if anything particularly egregious catches someone's eye, comments to that end are welcome.)
Environment:
Request Method: POST
Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['item.apps.ItemConfig',
'inventory.apps.InventoryConfig',
'fulfillment.apps.FulfillmentConfig',
'manifest.apps.ManifestConfig',
'order.apps.OrderConfig',
'utils.apps.UtilsConfig',
'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 "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in get_success_url
116. url = self.object.get_absolute_url()
During handling of the above exception ('ItemsTestCollection' object has no attribute 'get_absolute_url'), another exception occurred:
File "C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in post
172. return super().post(request, *args, **kwargs)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in post
142. return self.form_valid(form)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in form_valid
126. return super().form_valid(form)
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in form_valid
57. return HttpResponseRedirect(self.get_success_url())
File "C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" in get_success_url
119. "No URL to redirect to. Either provide a url or define"
Exception Type: ImproperlyConfigured at /utils/box-optimization/add-items/75
Exception Value: No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.