21π
Your approach of overriding the post
method and checking to see if the cancel button was pressed is ok. You can redirect by returning an HttpResponseRedirect
instance.
from django.http import HttpResponseRedirect
class AuthorDelete(DeleteView):
model = Author
success_url = reverse_lazy('author-list')
def post(self, request, *args, **kwargs):
if "cancel" in request.POST:
url = self.get_success_url()
return HttpResponseRedirect(url)
else:
return super(AuthorDelete, self).post(request, *args, **kwargs)
Iβve used get_success_url()
to be generic, its default implementation is to return self.success_url
.
16π
Why donβt you simply put a βCancelβ link to the success_url
instead of a button? You can always style it with CSS to make it look like a button.
This has the advantage of not using the POST form for simple redirection, which can confuse search engines and breaks the Web model. Also, you donβt need to modify the Python code.
- [Django]-Switching to PostgreSQL fails loading datadump
- [Django]-Django-taggit β how do I display the tags related to each record
- [Django]-Django filter JSONField list of dicts
12π
If using CBVβs you can access the view
directly from the template
<a href="{{ view.get_success_url }}" class="btn btn-default">Cancel</a>
Note: you should access it through the getter in case it has been subclassed.
This is noted in the ContextMixin docs
The template context of all class-based generic views include a
view
variable that points to the View instance.
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Django β How to use decorator in class-based view methods?
- [Django]-Django/DRF β 405 Method not allowed on DELETE operation
1π
Having an element of type button, will not send a POST request. Therefore, you can use this to do a http redirection like this:
<button type="button" onclick="location.href='{{ BASE_URL }}replace-with-url-to-redirect-to/'">Cancel</button>
- [Django]-Google Static Maps URL length limit
- [Django]-Django REST Framework : "This field is required." with required=False and unique_together
- [Django]-Django FileField upload is not working for me
0π
This is probably the easiest way to do what OP asks:
<input type="submit" value="Confirm" />
* <a href="{% url 'success_url' %}"><button type="button">Cancel</button></a>
- [Django]-Specifying limit and offset in Django QuerySet wont work
- [Django]-Django multiple template inheritance β is this the right style?
- [Django]-Django: how to do calculation inside the template html page?
-1π
Do you even need the get_success_url, why not just use:
Cancel
and go to any other url you want?
- [Django]-How do I match the question mark character in a Django URL?
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.