Thank you for the clear explanation! It works beautifully now.

On Friday, July 13, 2018 at 9:17:37 AM UTC-4, Daniel Roseman wrote:
>
> On Thursday, 12 July 2018 21:04:25 UTC+1, clavie...@gmail.com wrote:
>>
>> 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.)
>>
>
>
> You've misdiagnosed the problem. formaction is working fine; the data is 
> being posted to your add-items view. The issue is what happens next. Any 
> successful POST should be followed by a redirect, and CreateView by default 
> uses the value of get_absolute_url to determine where to direct to.
>
> Actually, formaction isn't what you want here at all. You've almost 
> mentioned the solution by talking about overriding get_absolute_url based 
> on the HTML button; that's not quite it, what you actually need to do is to 
> override the method responsible for calling it, which is the view's 
> get_success_url.
>
> class AddItemsToEvaluateCreateView(CreateView):
>     ...
>     def get_success_url(self):
>         if 'add_more' in self.request.POST:
>             return reverse('evaluate', kwargs={'pk': self.kwargs['pk']}
>         elif 'optimize' in self.request.POST:
>             return reverse('results', kwargs={'pk': self.kwargs['pk']}
>         return '/'
>
>
> Also you'll need to change your input elements; they don't need 
> `formaction`, but they do need a `name` so that they are sent in the POST 
> data:
>
>     <input type="submit" name="add_more" value="Add more items" />
>     <input type="submit" name="optimize" value="Optimize" />
>
> --
> DR.
>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a47aef1d-0dcd-4d35-9f0f-aaa0ba43c8ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to