I have a view:
class ProductList(SingleTableView):
model = Product
template_name = "app/product_list.html"
table_class = ProductTable
where in every row there is a button that should perform function()
:
class ButtonColumn(tables2.Column):
empty_values = list()
def render(self, value, record):
return mark_safe(f"""<a href="/link/{record.id}/"><button class="btn btn-info">link</button></a>""")
this ButtonColumn
provides a button and once it is clicked:
path("link/<int:pk>", views.LinkView.as_view(), name="link"),
And the corresponding view:
class LinkView(TemplateView):
model = Product
template_name = "app/product_list.html"
def get_success_url(self):
return reverse_lazy('product-list')
def get_context_data(self, **kwargs):
context = super(LinkView, self).get_context_data(**kwargs)
function[kwargs] # <------------------
context["table"] = Product.objects.all()
return(context)
My problem is with the Linkview - I want it to perform function
with some URL transmitted paramters and return to the former page (app/product_list.html
). How can I achieve this?
Modify the ButtonColumn
like this
class ButtonColumn(tables2.Column):
empty_values = list()
def render(self, value, record):
return mark_safe(f"""<a href="/link/{record.id}/?price=10"><button class="btn btn-info">link</button></a>""")
# There is no need to mention these query parameter in path.
The path will remain the same. No need to change anything.
path("link/<int:pk>", views.LinkView.as_view(), name="link"),
In you ListView
, you would use self.request.GET.get("param1", "")
where param1
is the URL transmitted parameter and ""
is the default value.
def get_context_data(self, **kwargs):
context = super(LinkView, self).get_context_data(**kwargs)
param1 = self.request.GET.get("param1", "")
# OR
function(self.request.GET) # extract required params in the function
function[kwargs] # <------------------
context["table"] = Product.objects.all()
return(context)
Note: Get is a dictionary
To redirect,
from django.shortcuts import redirects
# In your LinkView override get method
class LinkView(TemplateView):
def get(self, request, *args, **kwargs)
return redirect('some/url')