【问题标题】:Putting Python code in HTML files using Javascript使用 Javascript 将 Python 代码放入 HTML 文件中
【发布时间】:2018-08-07 11:23:09
【问题描述】:

我正在使用 Django 1.8 和 Python 3.5

这是我的 urls.py 文件

from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.conf import settings

urlpatterns = [
    # Examples:

    url(r'^$', 'mainpage.views.home2', name='home2'),
    url(r'^currency/(?P<currencywa>[\w]+)', 'currency.views.curr1', name='curr1'),

    url(r'^userpage/', 'login.views.userpage', name='userpage'),

] 

我正在尝试使用 Jquery for Django 创建链接

我想做的就是这个

&lt;a href="somelink"&gt;test&lt;/a&gt; // this resides in a table

使用 jquery

我希望使用 urlnames 而不是我希望使用的 url name='curr1'

var typein="<a href=\"{% url 'curr1'%}\">test</a>";

$('#cryptotable > tbody:last-child').append('<tr id=\"5\"><td>data1</td> </tr>');

我想在 django 中做同样的事情

&lt;a href="{% url 'home2'%}"&gt;test&lt;/a&gt;

但是当我点击这个时,我被重定向到http://localhost:8000/{% url 'curr1'%} 而不是http://localhost:8000/curr1}

如何使用 Django 方式使用 Jquery 制作 url 链接?

其他方面

我希望这样做(动态地使用 Jquery)-->

<html>
<body>
<a href='{% url "curr1" %}'>test</a>//put this duynamically using Jquery

</body>
</html>

【问题讨论】:

  • 您是否从与template.html 不同的资源加载您的JS 代码?否则,我不明白为什么您发布的代码不起作用。
  • 文件 myhtml.html 和 javascript 文件在同一台服务器上

标签: python jquery html django


【解决方案1】:

我只是为一般用例编写一个示例模板:

模板:

{% load staticfiles %}

<?xml version = "1.0" encoding = "UTF-8" ?>    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <title>sample</title>
    <script type="text/javascript" src="{% static 'jquery.min.js' %}"></script>
</head>

    <body>  
    </body>


</html>

<script>
    var typein = "<a href = {{url}}>Click Here</a>";
    $('body').append(typein);
</script>

查看:

from django.shortcuts import render_to_response
from django.template import RequestContext

def home(request):
    url = "home"
    return render_to_response('home.html', {'url' : url}, RequestContext(request))

网址:

from django.views.generic import TemplateView
from django.contrib.sitemaps.views import sitemap
from django.conf.urls import include, url
from django.contrib import admin

from views import *

urlpatterns = [
    url(r'^$', home),
    url(r'^home/', home),
    url(r'^robots.txt/', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
    url(r'^sitemap.xml/', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml'))
] 

【讨论】:

    【解决方案2】:

    url template tag 在服务器内部的模板渲染过程中需要转换为完整的 URL。一旦 HTML 到达客户端浏览器,javascript 就无法进行从 url 标记到完整 URL 的转换。

    您可以在 HTML 文档的隐藏部分中呈现 URL,以便 javascript 可以稍后(在浏览器中)访问该 URL 并将其放置在表格中,例如(如示例中的 other answer to this question 所示) .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 2015-04-17
      • 1970-01-01
      • 2020-05-21
      • 2021-04-09
      • 2023-03-09
      相关资源
      最近更新 更多