Reputation: 3765
The following view works as expected
class BrandEditView(PermissionRequiredMixin ,generic.UpdateView):
permission_required = 'change_brand'
template_name = 'brand_update_form.pug'
model = Brand
fields = ['name']
def get_object(self, queryset=None):
print(self.request.user)
self.object = Brand.objects.get(id=self.kwargs['pk'])
obj = Brand.objects.get(id=self.kwargs['pk'])
return obj
After form submission, how do I return to the same view (with the same object), but with context, like a message: "brand edited successfully"/"you can't do that"? I've found a way to redirect to the same view, but not with context.
Upvotes: 0
Views: 837
Reputation: 568
to use the message framework in class based view we use the SuccessMessageMixin
so update your view to be like this:
from django.contrib.messages.views import SuccessMessageMixin
class BrandEditView(SuccessMessageMixin, PermissionRequiredMixin ,generic.UpdateView):
permission_required = 'change_brand'
success_url = reverse_lazy('your redirect url')
template_name = 'brand_update_form.pug'
model = Brand
fields = ['name']
success_message = "created successfully!"
def get_object(self, queryset=None):
print(self.request.user)
self.object = Brand.objects.get(id=self.kwargs['pk'])
obj = Brand.objects.get(id=self.kwargs['pk'])
return obj
and in the base template or any template add this:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
{{ message }}
</li>
{% endfor %}
</ul>
{% endif %}
Upvotes: 1