HTML 中的 “"” 在 Django 中没有转换为 “'”。

huangapple go评论72阅读模式
英文:

HTML " not converting to ' in Django

问题

以下是翻译好的部分:

我正在创建一个Django应用程序,并尝试在按钮点击时对用户进行身份验证,然后根据其身份验证重定向用户。我有一个按钮用于登录用户,但我想根据他们的身份验证重定向用户。

代码如下:

<button type="submit" class="btn btn-primary" value="Login" onclick="location.href='{% url &quot;auth&quot; %}'">Login</button>

运行代码时,我收到以下错误:

TemplateSyntaxError 
无法解析余下的部分:'&quot;auth&quot;' from '&quot;auth&quot;'

这是因为'&quot;'没有按预期工作吗?
有人能否请解释一下这个问题?
英文:

I am creating a django application and was trying to authenticate users on a button click and redirect the users accordingly. I have a button that logs in the user, but I want to redirect the user based on their authentication.

The code is as follows:

&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;Login&quot; onclick = &quot;location.href=&#39;{% url &amp;quot;auth&amp;quot; %}&#39;&quot;&gt;Login&lt;/button&gt;

While running the code I get the error:

TemplateSyntaxError 
Could not parse the remainder: &#39;&amp;quot;auth&amp;quot;&#39; from &#39;&amp;quot;auth&amp;quot;&#39;

Is this because "&quot"; is not working as intended?
Could someone please shed some light on this?

答案1

得分: 2

移除&amp;quot;并将auth用作字符串文字:

<pre><code>&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;Login&quot; onclick=&quot;location.href='{% url &lt;b&gt;'auth'&lt;/b&gt; %}'&quot;&gt;Login&lt;/button&gt;</code></pre>

模板标签和模板过滤器由服务器端的*渲染引擎*解释,而不是由JavaScript解释,因此这发生在HTML被“解释”之前。
英文:

Remove the &amp;quot; and use auth as string literal:

<pre><code>&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;Login&quot; onclick=&quot;location.href='{% url <b>'auth'</b> %}'&quot;&gt;Login&lt;/button&gt;</code></pre>

the template tags and template filters are interpreted by the render engine at the server side, not by JavaScript, so this is before the HTML is "interpreted".

huangapple
  • 本文由 发表于 2023年7月11日 03:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定