python - Django - How to get current url from template and replace parameter value or delete parameter and append after
问题描述
In my django template I need to get the current url and modify a parameter value or add this parameter if not present.
here's an url example : /articles/pdf?perpage=2&page=2
All I know so far is how to retrieve the whole url :
{{ request.get_full_path }}
But what I need to do is to be able to create a new url that I will use as an href in which if "page" parameter present, I change its value, or if not present, I append this parameter to the url.
EDIT : Is it that hard that no one knows how to do this ?....
解决方案
因为这似乎是一个难题......我必须找到一种解决方法来实现它。
我剪切了所有形式的“page”参数,然后将新参数附加到 href 中:
{% define request.GET.page as page_num %}
{% define "?page="|add:page_num as page_first_param %}
{% define "&page="|add:page_num as page_next_param %}
{% define request.get_full_path|cut:page_first_param|cut:page_next_param as new_partial_url %}
{% if '?' in new_partial_url %} {% define "&" as p %} {% else %} {% define "?" as p %} {% endif %}
{% define new_partial_url|add:p as new_full_url %}
<a class="page-link" href="{{ new_full_url }}page={{ items.previous_page_number}}">
« Précédent</a>
推荐阅读
- vala - 如何将多编译器参数添加到 meson.build
- java - 在单独的 JSP 文件中调用 Connection 对象期间获取空值
- javascript - 当我通过数据数组进行映射时,如何避免文本块和/或 div 相互重叠
- flutter - 你如何在 2 个 Flutter 项目之间共享代码?
- r - 基于另一个矩阵创建矩阵包含具有 na 值的元素的索引
- reactjs - 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() => 状态属性)'
- php - 我应该使用哪种雄辩的关系?
- ios - 用户选择日期后无法显示“今天”而不是今天的日期
- c# - 带有 where 子句的 LINQ 方法语法
- c++ - C++中固定数组和动态分配数组的区别