简体   繁体   English

如何在 django get 请求中修改 url

[英]How to modify url in a django get request

I have a web page which displays some data (with get parameters to filter in the view) and I use a pagination.我有一个 web 页面,它显示一些数据(在视图中使用获取参数进行过滤)并且我使用了分页。

To go through the data i use a link to change the page in the paginator:通过数据到 go 我使用链接更改分页器中的页面:

<li class="paginator-data"><a href="{{ request.get_full_path }}?page={{ page_obj.next_page_number }}">Next</a></li>

Here is the view:这是视图:

@login_required
def search_and_export(request):

    # initialisation
    page_obj = Tree.objects.all()
    
    # FORM
    if request.method == 'GET':
        
        ## Here I get GET parameters or a default value like 
        
        data = str(request.GET.get('data', 1))
        page = str(request.GET.get('page', 1))
        

        ## creating a queryDict

        querydict = QueryDict('data='  data + '&page=' + page)
        big_request_form    = BigRequestForm(querydict)
        
        if big_request_form.is_valid():
            
            # Getting data from front endda
            data    = big_request_form.cleaned_data['data']
            # Extracting QuerySet
            page_obj    = filtering_function(data)

        
        paginator   = Paginator(page_obj,25)

        
        page_obj = paginator.get_page(page)
        page_obj.adjusted_elided_pages = paginator.get_elided_page_range(number = page, on_each_side=1, on_ends=1)

    else:
        big_request_form = BigRequestForm()
    
    # return values

    context = {
        'big_request_form'      : big_request_form,
        'page_obj'              : page_obj,
    }
    
    return render(request, 'filtered_stats.html', context)

Here is the problem: url looks like that after browsing a few pages:这是问题所在:浏览几页后,url 看起来像这样:

http://127.0.0.1:8000/?data=x&page=2&page=3&page=4&page=5&page=6

The view is working well (It displays me the last page) but I got this ugly link.该视图运行良好(它显示最后一页)但我得到了这个丑陋的链接。

I tried something like that:我试过这样的事情:

request.GET.get = querydict

(with querydict a QueryDict object with only one page data) But It didn't work. (使用 querydict a QueryDict object 只有一页数据)但它没有用。 I also thought of parsing data in the template but maybe there is a better way.我也考虑过在模板中解析数据,但也许有更好的方法。

What you want to do in HTML template is to get current URL. But actually you don't have to do it - relative path can be used instead.你想在 HTML 模板中做的是获取当前的 URL。但实际上你不必这样做 - 可以使用相对路径。 So just remove it:所以只需删除它:

<li class="paginator-data"><a href="?page={{ page_obj.next_page_number }}">Next</a></li>

The browser knows current URL and will use href attribute relative to it.浏览器知道当前 URL 并将使用与其相关的href属性。

UPD alternative - to keep other parameters in the URL is to construct "next URL" inside the view and pass it in context as next_url . UPD替代方案 - 在 URL 中保留其他参数是在视图内构造“下一个 URL”并将其作为next_url在上下文中传递。

Ie something like so:即是这样的:

In views :观点中:

 next_url = request.get_full_path()
 next_url.replace(f'page={current}', f'page={page_obj.next_page_number}')
 context['next_url'] = next_url

In template:在模板中:

<li class="paginator-data"><a href="{{ next_url }}">Next</a></li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM