javascript - 使用应用程序名称和命名空间在 django 模板 url 中动态添加 js 变量值

标签 javascript python django

I have multiple apps in project and using appname and namespace for urls. I need to dynamically add js variable value in django template url with app name. If I write without appname like --

var temp =23
var href = "{% url 'report_page' id=0 %}".replace(/0/, temp.toString())

It is giving desired result as--

"{% url 'report_page' location_id=23 %}"

But when I use appname its not working for me e.g

var href = "{% url 'appname : report_page' id=0 %}".replace(/0/, temp.toString())

Let me give you the exact code: In django template:

<html>
<a href="{% url 'appname : report_page' id=0 %}">Report</a>
</html>
var href = "{% url '应用程序名称:report_page' id=0 %}".replace(/0/, temp.toString())

In url.py of the appname:

appname = 'appname'
  urlpatterns = [
    url(r'^report-page$', views.report_page, name='report_page'),
    url(r'^report-page/(?P<id>[0-9]+)$', views.report_page, name='report_page'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

In views:

def report_page(request):
    import pdb;pdb.set_trace()
    # location_id = request.GET['id']
    return render(
        request,
        'report.html',
        {'id' : request.user.id, 'id' : ''}
    )

Note: I'm using django 1.11 and I have also tried below one but getting empty string:

var temp =$('#check_id').val() <a href="{% url 'appname: report_page' %}?id="+temp;>Report</a>

最佳答案

当您使用 djagno url 例如 {% url 'appname : report_page' id=0 %} 时,当 django 模板渲染时,它将转换为类似 /appname/报告页/0/, 它的解决方案是, 首先,您将获取 a 标签属性值,然后更改它。 请找到以下解决方案。

var arr = $("a").attr('href').split('/') arr[arr.length-2]=777 # 演示值 $("a").attr('href',arr.join('/'))

关于javascript - 使用应用程序名称和命名空间在 django 模板 url 中动态添加 js 变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56701687/

相关文章:

javascript - 如何在 mapbox gl 中以编程方式关闭所有弹出窗口?

Django celery SQS "No result backend is configured."

python - 在 Python 3 中使用 for 循环查找字符串中的值

python - 有没有办法在 Python 数据表中对整个帧执行算术运算?

python - 匹配 URL 中的点时出现奇怪的 Django 行为

python - Django:使用用户作为外键保存表单

javascript - Chartjs 3圆环图圆 Angular

javascript - javascript 中 Math.cos 函数将度数转换为弧度时出现问题

javascript - 如何在 Typescript 中导出具有其他名称的命名空间

python - 有没有办法强制将数据导入 Odoo 9 或 10 中的只读字段?