0

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 'appname : 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>
1
  • Thanks Ankit, The logic helped I made some changes though, id = $('#id').val() var arr = $('.set-select-volume').attr('href').split('/') arr[arr.length] = id.toString() $(".set-select-volume").attr('href', arr.join('/')) Commented Jun 25, 2019 at 8:41

1 Answer 1

1

When you are using with djagno url such as {% url 'appname : report_page' id=0 %}, when django template will rendering it will convert into the like /appname/report-page/0/, it's solutions is, first you will to take a tag attributes value then change this. please find below solutions.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ankit it helped me, just I change in your answer, it will not generate "/appname" only url will be there no appname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.