Calling HTML snippet in Javascript

Asked

Viewed 70 times

0

I am using Django Framework for an application that searches videos on youtube. But there is an excerpt of JS, which calls an html where "embeds" the video searched. And I don’t know how to create this route in Django.

JS searching the video

function tplawesome(e,t){res=e;for(var n=0;n<t.length;n++){res=res.replace(/\{\{(.*?)\}\}/g,function(e,r){return t[n][r]})}return res}

$(function() {
    $("form").on("submit", function(e) {
       e.preventDefault();
       // prepare the request
       var request = gapi.client.youtube.search.list({
            part: "snippet",
            type: "video",
            q: encodeURIComponent($("#search").val()).replace(/%20/g, "+"),
            maxResults: 3,
            order: "viewCount",
            publishedAfter: "2015-01-01T00:00:00Z"
       });
       // execute the request
       request.execute(function(response) {
          var results = response.result;
          $("#results").html("");
          $.each(results.items, function(index, item) {
            $.get("busca/item.html", function(data) {
                $("#results").append(tplawesome(data, [{"title":item.snippet.title, "videoid":item.id.videoId}]));
            });
          });
          resetVideoHeight();
       });
    });

    $(window).on("resize", resetVideoHeight);
});

HTML that JS CALLS

<div class="item">
    <h2>{{title}}</h2>
    <iframe class="video w100" width="640" height="360" src="//www.youtube.com/embed/{{videoid}}" frameborder="0" allowfullscreen></iframe>
</div>

Views.py file where I create the path for the HTML file to be rendered

def busca(request):
    return render(request, 'busca/item.html')

When I do the search, you give 404 in the search/item.html

  • search is a folder, Django does not allow this call... from 404 @Noobsaibot

  • Yes. urls.py from the search app: path('search/', views.search, name='search')

  • I want to pass the path where ta HTML, which will be rendered to soak the video. Not necessarily will open a new page

  • I still think the jQuery url should be: quest/ was as you defined in the urls file

  • 1

    That’s right @Noobsaibot, Thanks for the help

  • Remember: path(rota, view, ...) like this in documentation, in that question also explains.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.