问题描述
class ProductList(SingleTableView):
model = Product
template_name = "app/product_list.html"
table_class = ProductTable
每行中都有一个应该执行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>""")
此ButtonColumn
提供了一个按钮,一旦单击它:
path("link/<int:pk>",views.LinkView.as_view(),name="link"),
以及相应的视图:
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)
我的问题是链接视图-我希望它对某些URL传输的参数执行function
,然后返回到前一页(app/product_list.html
)。我该如何实现?
解决方法
像这样修改keras
ButtonColumn
路径将保持不变。 无需更改任何内容。
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.
在 path("link/<int:pk>",views.LinkView.as_view(),name="link"),
中,您将使用ListView
,其中self.request.GET.get("param1","")
是URL传输参数,而param1
是默认值。
""
注意:获取是字典
要重定向,
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)