简体   繁体   English

如何使整个 div 可点击并将动态 url 放入 Django 模板中?

[英]How to make whole div clickable and put a dynamic url in Django template?

I am trying to display a post (like a tweet in twitter) and when I display it I want the whole div to be clickable.我正在尝试显示一个帖子(就像 Twitter 中的一条推文),当我显示它时,我希望整个 div 都可以点击。 So this works.所以这有效。 Website is working the way I wanted with this code;网站正在按照我想要的方式使用此代码;

<div onclick='location.href="{% url 'post' post.id %}";' style="cursor: pointer;"> 
  <a href="#"> some other links </a> // it will take user to another page
  <img src= "" > //some image
  <p> some content </p>
  <a href="#"> some other links </a>
</div>

But it gives me some warnings in the terminal (VScode), each of the error is showing the same thing (div onclick line ) but strangely giving different errors.但它在终端(VScode)中给了我一些警告,每个错误都显示相同的东西(div onclick 行)但奇怪地给出了不同的错误。 And I don't use javascript for this part of the code.而且我在这部分代码中没有使用 javascript。

I tried different syntax like changing " " to ''.我尝试了不同的语法,例如将“”更改为“”。 but it doesn't solve the problem.但这并不能解决问题。

在此处输入图像描述

Also in the code, it shows the line with red color as below, if it helps to figure what the problem is.同样在代码中,它显示了红色的线,如下所示,如果它有助于找出问题所在。

在此处输入图像描述

Any advice would be appreciated.任何意见,将不胜感激。 Thanks in advance.提前致谢。

Your code is absolutely correct ignore the warnings and red-underlines by VScode.您的代码绝对正确,请忽略 VScode 的警告和红色下划线。 Red-underlines are there because by default the language mode is HTML therefore sometimes use of '' and "" shows error but it is fine as per Django Templating Engine有红色下划线是因为默认情况下语言模式是 HTML 因此有时使用''""会显示错误但根据Django Templating Engine

If for any reason onclick='location.href="{% url 'post' post.id %}";'如果出于任何原因onclick='location.href="{% url 'post' post.id %}";' does not work use this onclick='location.href="post/{{post.id}}";'不工作使用这个onclick='location.href="post/{{post.id}}";'

before your "div" tag, put an "a" tag and give it the url. You don't need to repeat the url manytimes.在你的“div”标签之前,放一个“a”标签并给它url。你不需要多次重复url。

EXAMPLE:例子:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM