最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - how to pass javascript variable value to views.py function in django? - Stack Overflow

programmeradmin0浏览0评论

I am trying to send a string generated from javascript tag in my html page to views.py function. I'll use that value to render it to another html page where another javascript will use it.

This is the variable which parses json data:

var url = data.result.docs[i].source.enriched.url.url;

I want to send "url" to my function in views.py but i am not able to figure out a way.

Also i want to keep a link, so that when user click it the "url" is passed to views.py and my function there send it to other html file.

sjd += "<br/>" + data.result.docs[i].source.enriched.url.title +
            '<a href="/rawHtmlText/url">' + ' Read more' + '</a>' + '<br>'

This is kind of link i would like to have in my page. I know urls.py should have a proper pattern, but i am not able to figure it out.

May be jQuery can be used but I am a noob in this field so kind of stuck here.

Please guide me.

I am trying to send a string generated from javascript tag in my html page to views.py function. I'll use that value to render it to another html page where another javascript will use it.

This is the variable which parses json data:

var url = data.result.docs[i].source.enriched.url.url;

I want to send "url" to my function in views.py but i am not able to figure out a way.

Also i want to keep a link, so that when user click it the "url" is passed to views.py and my function there send it to other html file.

sjd += "<br/>" + data.result.docs[i].source.enriched.url.title +
            '<a href="/rawHtmlText/url">' + ' Read more' + '</a>' + '<br>'

This is kind of link i would like to have in my page. I know urls.py should have a proper pattern, but i am not able to figure it out.

May be jQuery can be used but I am a noob in this field so kind of stuck here.

Please guide me.

Share Improve this question edited Dec 8, 2015 at 3:10 Sangit Dhanani asked Dec 8, 2015 at 3:03 Sangit DhananiSangit Dhanani 531 silver badge9 bronze badges 2
  • you can have a route variable in django like GET "/rawHtmlText/:url", and you can grab url there. I'm not familiar with Django, but I'm sure it should have a facility to grab variables from routes. – Prashanth Chandra Commented Dec 8, 2015 at 3:10
  • i am confused doing that cz of trying out different solutions of different posts but none of them working – Sangit Dhanani Commented Dec 8, 2015 at 3:12
Add a ment  | 

2 Answers 2

Reset to default 6

The easiest way is probably to just build an AJAX query. Try something like this:

<script type="text/javascript">
    $(document).ready(function() {
        var url = data.result.docs[i].source.enriched.url.url;
        $("#send-my-url-to-django-button").click(function() {
            $.ajax({
                url: "/process_url_from_client",
                type: "POST",
                dataType: "json",
                data: {
                    url: url,
                    csrfmiddlewaretoken: '{{ csrf_token }}'
                    },
                success : function(json) {
                    alert("Successfully sent the URL to Django");
                },
                error : function(xhr,errmsg,err) {
                    alert("Could not send URL to Django. Error: " + xhr.status + ": " + xhr.responseText);
                }
            });
        });
    });
</script>

Assuming you have a button on your page like this:

<button type="button" id="send-my-url-to-django-button">Send URL to Django View</button>

Clicking that button should send the url to your Django view. Then on the Django side, you can access the url like so:

def process_url_from_client(request):
    url = request.POST.get('url')

Yes you can use anchor tag only, but in that case you will send data like this
http://host/path_to_view.py/urlValueToSend. This will be a simple get request and url will be sent to the server You may need ro encode the url as it contains special characters

var val = EncodeURI(url);
发布评论

评论列表(0)

  1. 暂无评论