我正在使用 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 创建链接
我想做的是这个
<a href="somelink">test</a> // this resides in a table
使用jquery
I wish to use urlnames instead of url that is I wish to use name='curr1'
var typein="<a href=\"{% url 'curr1'%}\">test</a>";
$('#cryptotable > tbody:last-child').append('<tr id=\"5\"><td>data1</td> </tr>');
我想在 django 中做同样的事情
<a href="{% url 'home2'%}">test</a>
但是当我点击它时,我被重定向到 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>
最佳答案
我只是在为一般用例编写示例模板:
模板:
{% 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'))
]
关于python - 使用 Javascript 将 Python 代码放入 HTML 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49015016/