我正在使用Django构建网站。我试图传递来自index.html
的输入,并使用about.html
在view.py
中显示它。
我的输入似乎在浏览器顶部的url中传递了,我试图将此值存储在变量中并在html段落中显示该变量。但是它没有显示。而不是看到与变量关联的输入,我只是看到带有变量名称的文本字符串。
我的index.html
:
<form action="{% url 'theaboutpage' %}">
<input type="text" name="user_input">
<input type="submit" value="click here now">
</form>
我的about.html
:
<a href={% url 'thehomepage' %}>go back to the home page</a>
<p>{{'input_from_home_page'}}</p>
我的views.py
:
def about(request):
Hellothere = request.GET['user_input']
return render(request, 'about.html', {'input_from_home_page':Hellothere})
答案 0 :(得分:2)
只需删除模板中about.html
处变量的引号即可。它应该看起来像这样:
<p>{{ input_from_home_page }}</p>
作为旁注:
<input type="text" name="user_input">
中输入的信息是敏感信息,则应考虑使用“ POST” HTTP方法而不是“ GET”传递它。在这种情况下,请记住包括{% csrf_token %}
。要获取传递的信息,您可以在视图中使用:request.POST.get('user_input')
。hello_there
而不是Hellothere
。