2

I am using Django 1.8 and Python 3.5

This is my urls.py file

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'),

] 

I am trying to make links using Jquery for Django

what I want to do is this

<a href="somelink">test</a> // this resides in a table

using 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>');

I want to do this equivalent in django

<a href="{% url 'home2'%}">test</a>

But when I click on this ,I get redirected to http://localhost:8000/{% url 'curr1'%} instead of http://localhost:8000/curr1}

What do I do to use Django way of url links made using Jquery ?

IN OTHER WORDS

I wish to do this (Dynamically using Jquery)-->

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

</body>
</html>
2
  • Are you loading your JS code from a different resource than the template.html? Otherwise, I don't see why your posted code doesn't work. Commented Feb 27, 2018 at 18:39
  • Both the files myhtml.html and the javascript files are on the same server Commented Feb 27, 2018 at 19:21

2 Answers 2

1

I am just writing a sample template for general use case:

Template:

{% 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>

View:

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))

URL:

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'))
] 
Sign up to request clarification or add additional context in comments.

Comments

0

The url template tag needs to be converted to a full URL during template rendering inside the server. Once the HTML reaches the client browser, javascript cannot make that transformation from url tag to full URL.

You could render the URL in a hidden part of your HTML document, so that javascript can access the URL later (in the browser) and place it inside a table, for example (like the other answer to this question shows in the example).

Comments

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.