我使用Django 1.8和Python3.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为Django创建链接。
我想做的是
<a href="somelink">test</a> // this resides in a table
使用jquery
我希望用urlname代替url,也就是说,我希望使用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}。
如何使用Jquery生成的url链接的Django方式?
,换句话说,
我希望这样做(动态使用Jquery)->
<html>
<body>
<a href='{% url "curr1" %}'>test</a>//put this duynamically using Jquery
</body>
</html>发布于 2018-02-27 18:49:01
我只是为一般用例编写一个示例模板:
模板:
{% 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'))
] 发布于 2018-02-27 23:49:10
在服务器内部的模板呈现过程中,需要将 template tag转换为完整的URL。一旦HTML到达客户端浏览器,javascript就无法进行从URL标记到完整url的转换。
您可以在hidden的隐藏部分呈现URL,以便javascript可以稍后(在浏览器中)访问该URL,并将其放置在表中,例如(如示例中的other answer to this question所示)。
https://stackoverflow.com/questions/49015016
复制相似问题