返回按钮存储以前的数据DJANGO

问题描述 投票:0回答:1

我正在使用django创建一个应用,用户可以在其中搜索某些内容的定义。所以他正在输入“安全性”一词以获取对此的定义。他得到结果,然后按后退按钮。然后他获得了一个网站,但是搜索字段仍然存储了旧数据/输入的“安全性”。我该如何解决?

模板:

<div>
    <h1><a href="/">Drink drank drunk</a></h1>
</div>
<h1>Jakie masz skladniki?</h1>

<form action="{% url 'search_results' %}" method="get">
  <input name="q" type="text" placeholder="Search...">
</form>
{% if messages %}
<ul class="messages">
  {% for message in messages %}
  <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
  {% endfor %}
</ul>
{% endif %}

观看次数:

from django.shortcuts import render
from django.db.models import Q #new

from .models import Recipe
from .models import Ingredient

from django.contrib import messages
from django.shortcuts import redirect

def drink_list(request):
    template = "drinks/drink_list.html"
    return render(request, template)

def search_results(besos):

    query = besos.GET.get('q')
    if not query or query == ' ' or query == '  ' or query == '   ':
    #how to write this ^  in shortest way? if string is empty then return 'drink_list'
        messages.error(besos, "Search field can not be empty")
        return redirect('drink_list')

    else:
        q = Q()
        for queries in query.split():
            q |= (Q(ingredients__ingredient_name__icontains=queries))
            #why it look for 'sok z cytryny' and show as well sok z limonki
        results = Recipe.objects.filter(q)
        template = "drinks/search_results.html"
        context = {
        'results' : results,
        }
        return render(besos, template, context)

URL:

urlpatterns = [
    path('', views.drink_list, name='drink_list'),
    path('search/', views.search_results, name='search_results'),
    path('no_name/', views.drink_list, name='drink_list'),
]
django google-chrome button back
1个回答
0
投票

如何使用一些JavaScript尝试一下呢?您可以在页面加载时清空输入标签。

<script>
    window.onload = function(){
                        document.getElementByName("q").value = "";
                    }
</script>
© www.soinside.com 2019 - 2024. All rights reserved.