Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Django模板设置默认值_Python_Django_Forms_Django Templates - Fatal编程技术网

Python 使用Django模板设置默认值

Python 使用Django模板设置默认值,python,django,forms,django-templates,Python,Django,Forms,Django Templates,因此,我正在创建一个搜索函数,我希望在呈现搜索结果时,先前输入的查询保留在搜索框中。目前我这样做的方式是发送POST请求,获取查询并将其作为变量发送回模板。但出于某种原因,它只对查询的第一个单词有效,而所有后续单词都会被删除。但是,当我在标记中呈现相同的变量时,结果与我预期的一样。有什么我做得不对的吗 <div id="searchwrapper"> <form action="/search" method="post"> {% csrf_token %}

因此,我正在创建一个搜索函数,我希望在呈现搜索结果时,先前输入的查询保留在搜索框中。目前我这样做的方式是发送POST请求,获取查询并将其作为变量发送回模板。但出于某种原因,它只对查询的第一个单词有效,而所有后续单词都会被删除。但是,当我在标记中呈现相同的变量时,结果与我预期的一样。有什么我做得不对的吗

<div id="searchwrapper">
    <form action="/search" method="post"> {% csrf_token %}
        {% if old_query %}
        <input type="text" class="searchbox" name="s" value={{old_query}} />
        {% else %}
        <input type="text" class="searchbox" name="s" value="" />
        {% endif %}
        <input type="image" src="static/images/search-icon.svg" class="searchbox_submit" value="" />
    </form>
</div>


def search(request):
    context = {}
    context.update((csrf(request)))
    results_string = ""
    if request.POST:
        results_string = find(request)
        old_query = request.POST['s']
        context.update({"old_query": old_query})
    search_bar = render_to_string("search.html", Context(context))
return HttpResponse(search_bar + results_string)

{%csrf_令牌%}
{%if旧的_查询%}
{%else%}
{%endif%}
def搜索(请求):
上下文={}
更新((csrf(请求)))
结果_string=“”
如有要求,请发送:
结果\u字符串=查找(请求)
old_query=request.POST['s']
update({“old_query”:old_query})
search\u bar=render\u to\u字符串(“search.html”,Context(Context))
返回HttpResponse(搜索栏+结果字符串)
我不认为find方法是相关的,但是如果你认为它有用,请告诉我,我可以发布它。模板是“search.html”的相关部分,如我所说,如果我将
{{old_query}}

行添加到
{%if old_query%}
部分,将显示正确的值,但目前如果我使用了
hello stackoverflow这样的查询我只会得到“hello”作为搜索字段的值


这可能有点傻,但我对web开发人员来说是个新手,因此非常感谢您的帮助。

修复这行代码,将
{{{old_query}}}
括在引号之间:

<input type="text" class="searchbox" name="s" value="{{old_query}}" />


这将为您提供整个搜索,而不是第一个单词。

修复此行以在引号之间换行
{{old_query}}

<input type="text" class="searchbox" name="s" value="{{old_query}}" />


这将为您提供整个搜索,而不是第一个单词。

哦,太棒了!假设它像shell/perl中的变量一样工作,对吗?
{{{var}
表示法所做的是输出对象
\uuuuuunicode\uuuu
\uuurepr\uu
方法。剩下的是普通的html,所以你需要引号:)哦,太棒了!假设它像shell/perl中的变量一样工作,对吗?
{{{var}
表示法所做的是输出对象
\uuuuuunicode\uuuu
\uuurepr\uu
方法。其余的是普通html,所以您需要引号:)为什么不使用django表单?这将为您节省大量工作,正如我所说,我对webdev非常陌生,我非常重视在进入库之前深入了解引擎盖下发生的事情。稍后我可能会切换到django表单,但这让我很清楚内部的阴谋。为什么不使用django表单?这将为您节省大量工作,正如我所说,我对webdev非常陌生,我非常重视在进入库之前深入了解引擎盖下发生的事情。稍后我可能会切换到django表单,但这让我非常清楚内部的阴谋。