【问题标题】:Django input not showingDjango输入未显示
【发布时间】:2020-09-12 11:19:39
【问题描述】:

我正在使用 Django 构建网站。我正在尝试从index.html 传递输入并使用view.pyabout.html 中显示它。 我的输入似乎被传递,因为它位于浏览器顶部的 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})

【问题讨论】:

  • 更多详情请参阅此讨论 - Link

标签: python html django python-3.x


【解决方案1】:

只需删除模板中变量about.html 的引号即可。它应该是这样的:

<p>{{ input_from_home_page }}</p>

附注:

  1. 如果在&lt;input type="text" name="user_input"&gt; 中输入的信息是敏感信息,那么您应该考虑使用“POST”HTTP 方法而不是“GET”来传递它。在这种情况下,请记住包含{% csrf_token %}。要获取传递的信息,您可以在视图中使用:request.POST.get('user_input')
  2. convention,您应该使用小写字母命名变量。在您的情况下,最好使用 hello_there 而不是 Hellothere

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2021-04-01
    • 2016-10-31
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多