简体   繁体   English

使用 Javascript 将 url 插入到 Django 模板中

[英]Using Javascript to insert url into Django template

I made a map application using Mapbox, where a user can click on a feature and a popup shows with details about the feature.我使用 Mapbox 制作了一个地图应用程序,用户可以在其中单击一个功能,然后弹出窗口显示有关该功能的详细信息。 I would also like to provide a link in the popup to the DetailView of the object using Javascript, but I'm having trouble generating the anchor tag.我还想在弹出窗口中使用 Javascript 提供一个指向对象的 DetailView 的链接,但是我在生成锚标记时遇到了问题。 So far I have到目前为止我有

new mapboxgl.Popup()
  .setLngLat(e.lngLat)
  .setHTML('<a href="{% url app_name:view pk=foo %}">To Detail View</a>')
  .addTo(map);

But when I try it out on the development server, it gives me a 404 error with a request url of http://127.0.0.1:8000/map/%7B%25%20url%20app_name:view%20pk%3Dfoo%20%25%7D但是当我在开发服务器上尝试时,它给了我一个 404 错误,请求 url 为http://127.0.0.1:8000/map/%7B%25%20url%20app_name:view%20pk%3Dfoo%20 %25%7D

How do I get it to pass the correct url?我如何让它传递正确的网址? I've also tried inserting the link into a normal HTML div and I get the same problem.我也试过将链接插入普通的 HTML div 中,但我遇到了同样的问题。

It looks like its not recognizing your template tag as a template tag.看起来它没有将您的模板标签识别为模板标签。 Your syntax looks fine.你的语法看起来不错。 Are you trying to use the url template tag in a javascript file?您是否尝试在 javascript 文件中使用 url 模板标记? If so it wont work.如果是这样就行不通了。 You can only use template tags in the html files.您只能在 html 文件中使用模板标签。

If this is in a html file, can you post the full code?如果这是在一个html文件中,你能发布完整的代码吗?

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

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