How to pass data from one file to other in python?

89 views Asked by At

My html code is (ser.html)

<div>
<a v-bind:href="'ser/'+post.name">
{{post.name}}</a>
</div>

I need to pass post.name from this file to other ie.(det.html)

My views.py is

from django.shortcuts import render

import os
def ser(request):
    return render(request,"ser.html",{});
def det(request):
    return render(request,"det.html",{'name' :name});

My urls.py is

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^abt', views.abt),
    url(r'^$', views.index),
    url(r'^ser',views.ser),
    url(r'^det',views.det),
    ]

My det.html has following code

<script>

    mounted() {

    var self = this; 

     $.ajax({
                  url: 'post/filter/',
                  data: {
                    category: {{name}},

                  },
                  type: "POST",
                  dataType: 'json',

     success: function (e) { 
      if (e.status == 1) { 
      window.searchContent.vector = e.data;
                    console.log(e);
    }
    }
    });
    </script>

I need to pass 'name' to this det.html category in order to fire the ajax request. But when I do this way I am getting error. This is my project using python and vue js. Can anybody please help me?

Error is:

missing 1 required positional argument: 'name'
1

There are 1 answers

8
CM. On BEST ANSWER

In your view.py file the render has no clue where to get the name variable from. Here is one way to fix it: You can pass the name parameter from ser.html to the backend as GET param:

<div>
<a v-bind:href="'searchnew/?name='+post.name">
{{post.name}}</a>
</div>

Then in your view.py file, get the name variable:

def det(request):
    name = request.GET.get('name')
    return render(request,"det.html",{'name' :name})

In det.html it will be available.


If you'd like to do it with POST request, change the ser.html to run a VueJS function, something like this:

<div>
<a v-on:click="funcToPost">{{post.name}}</a>
</div>

In the VueJS object implement the method funcToPost to do the POST call, something like this:

var app = new Vue({
  // ... some code here
  methods: {
    funcToPost: function() {
      // ... some code here
      $.ajax({
        // ... some code here
        data: {
          name: {{name}}
        }
      })
    }
  }
})

In view.py change request.GET.get('name') to request.POST.get('name').

Hope this solves your problem. Good luck.