简体   繁体   English

如何在JS上运行python脚本(按钮onclick)

[英]How to run python script on JS(button onclick)

How to run django script in js. 如何在js中运行django脚本 I need to use IMDb API on onclick method to get id of movie user is searching. 我需要在onclick方法上使用IMDb API来获取正在搜索的电影用户的ID。

how can I do it? 我该怎么做?

I am still begginer in django so dont judge me :) 我还是在Django的乞gg,所以不要评判我:)

Here is my html for that part: 这是我的那部分的HTML:

 <div class="form-inline mt-2 mt-md-0">
    <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search" id="search_movie_textField">
    <button class="btn btn-outline-success my-2 my-sm-0" onclick="search_movie()" >Search</button>
  </div>

Here is my JS I am trying to write: 这是我要编写的JS:

function search_movie(){
var sreach = document.getElementById("search_movie_textField").value;

id = //GET IMDB ID FROM SEARCH

window.location.href = '/movielist/movies/' + id; }

Try this: 尝试这个:

function search_movie() {
    var search = document.getElementById("search_movie_textField").value;
    window.location.href = '/movielist/movies/' + search;
}

Codepen: https://codepen.io/anon/pen/bKxGbg Codepen: https ://codepen.io/anon/pen/bKxGbg

You should use ajax for this. 您应该为此使用ajax。 If your directory structure is something like this, you can do the following. 如果您的目录结构是这样的,则可以执行以下操作。

your_django_app/
    __init__.py
    models.py
    urls.py
    views.py
    templates/
        your_django_app/
            search.html
    ....

Add a url in your urls.py for the search function 在您的urls.py添加一个用于搜索功能的网址

from django.urls import path
from . import views

app_name = "your_app_name"

urlpatterns = [
    .....
    path('search', views.search, name='search'),
    .....
]

And in the views.py write the function which will search for the movie ID. 然后在views.py编写搜索电影ID的函数。

from django.http import JsonResponse

def search(request):
    search_keyword = request.POST["search"]
    ......
    id = get_movie_id(search) # Your function to obtain the movie ID
    ......
    return JsonResponse({"id": id})

And finally the search.html should be something like this: 最后search.html应该是这样的:

<div class="form-inline mt-2 mt-md-0">
    <form id="search-form">
        {% csrf_token %}
        <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search" id="search_movie_textField" name="search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
</div>

<script>
$("#search-form").on("submit",function(e){
    e.preventDefault();
    $.ajax({
        url : '{% url "your_django_app:search" %}',
        type: "POST",
        data: $(this).serialize(),
        success: function (data) {
            window.location.href = '/movielist/movies/' + data.id;
        },
        error: function (jXHR, textStatus, errorThrown) {
          alert(errorThrown);
        }
    });
});
</script>

Note: Don't forget to include jQuery 注意:别忘了包含jQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM