Django -如何重定向回搜索页面并包含查询词?

ebdffaop  于 2023-03-09  发布在  Go
关注(0)|答案(2)|浏览(531)

我的页面/视图结构如下:

index.html => search_results.html => object_details.html,
in which user can make a POST request in object_details.html.

我尝试在POST请求后重定向回search_results.html,但同时保留两个查询字符串。
搜索结果的网址如下所示,可以进行一次或两次查询:

www.example.com/search-results/?search_post=xxxxx&search_author=xxxxx

我能得到我想要的最接近的方法是使用:

return redirect(reverse('search-results') + '?' + 'search_post={{search_post}}'+'&'+'search_author={{search_author}}')

然而,这实际上将在search_results.html中的搜索表单的2个字段中的每一个中显示“{{search_post}}”和“{{search_author}}”,并且将返回URL:

**一个

另外,我的search_results视图包含以下内容:

search_post = request.GET.get('search_post')

search_author = request.GET.get('search_author')

我该怎么做?尝试使用类似问题的答案,但尚未弄清楚。谢谢

2vuwiymt

2vuwiymt1#

views.py

def index(request):
    return render(request, 'index.html')

def search_results(request, *args, **kwargs):

    search_post = request.GET.get('search_post')
    search_author = request.GET.get('search_author')

    if search_author and not search_post:
        results = Post.objects.distinct().filter(
            (Q(city__icontains=search_author.strip())))
    
    elif search_post and not search_author:
        results = Post.objects.distinct().filter(
            (Q_post__icontains=search_post.strip()))

    elif search_post and search_author:
        results = Post.objects.distinct().filter(
            (Q_post__icontains=search_post.strip())
            )
            .filter(
            (Q(city__icontains=search_author.strip())
            )
            )
    
    elif search_post and search_author == None:
        results = Post.objects.all().distinct()
        return render(request, 'search_results.html', {'results':results, 'search_post':search_post, 'search_author':search_author})
    
    else:
        results = Post.objects.all().distinct()

    return render(request, 'search_results.html', {'results':results, 'search_post':search_post, 'search_author':search_author})


def post_details(request, pk):

    post = Post.objects.get(pk=pk)
    post_owner = post.owner
    search_post = request.GET.get('search_post')
    search_author = request.GET.get('search_author')

    if request.user.is_authenticated:
        saved = Submission.objects.filter(post=post, owner=request.user)
        if saved and request.method == 'POST':
            return render (request, 'post_details.html', {'post':post, 'saved':saved})

        if request.method == 'POST':
            instance = Submission(post=post, post_owner=post_owner,owner=request.user)
            instance.save()
            return redirect('post-details', pk=post.id)

    else:
        return render (request, 'post_details.html', {'post':post})
    
    return render (request, 'post_details.html', {'post':post, 'saved':saved})
x7rlezfr

x7rlezfr2#

你只是在引号上犯了一个错误。你不能使用与django模板相同的语法。尝试使用f字符串:

return redirect(reverse('search-results') + f"?search_post={search_post}&search_author={search_author}")

关于字符串的文档:https://peps.python.org/pep-0498/
好的,在你回答之后,我对你的视图做了一个重构。你可以在你的表单处理的每一步重用results来进行下一步的过滤。而且你的,我认为,你的elif链接不好,出错了。

def search_results(request, *args, **kwargs):

    search_post = request.GET.get('search_post', '')
    search_author = request.GET.get('search_author', '')

    results = Post.objects.all()
    if search_author != '':
        results = results.filter(city__icontains=search_author.strip())
    
    if search_post != '':
        results = results.filter(post__icontains=search_post.strip())

    context = {
        'results': results,
        'search_post': search_post,
        'search_author': search_author,
    }

    return render(request, 'search_results.html', context)

相关问题