0

I am trying to get the id of a model object of django from the template using ajax. But I cannot get the exact value of data in the views.py.I am getting a None value for id in views. Where I have done wrong? Here is my code Views.py:



def is_ajax(request):
    return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

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

def index(request):    
    # If you have passed any data through the 'data' property 
    #you can get it here, according to the method used.
    ##if is_ajax(request=request):
        id = request.GET.get('data')
        msg = testing.objects.exclude(id = id).first()
    # Note that we are passing the 'ajax_comments.html' template.
        #return render(request, 'practice.html', {'text': msg, 'id':id})
    ##else:
        ##msg = testing.objects.last()
        return render(request, 'practice.html', {'text': msg, 'id': id} )       

    #comments = Comment.objects.all.first()
    #return render(request, 'practice.html', {'comments': comments})

Template:

{% extends 'base.html'%}

{%block head %} {%endblock%}

{%block navbar %} {% endblock %}

{%block navsmall %} {% endblock %}

{% block header%}

{% endblock %}

{% block firstgrid%}

{%endblock%}
{%block secondgrid%}
<div id = 'myTest'>
{{text.text}} <br> {{id}}
<button class = "asd" value="{{text.td}}">Button</button>
</div>
<script>
      $(".asd").click(function(e){
        e.preventDefault();
        var id = $(this).val();
        $.ajax({
            method: 'GET',  // defaults to GET. Set POST if you like, but be aware of the CSRF token submission too!
            url: "{% url 'practice' %}",  // submit to the same url
            data: {'data': id},  // pass here any data to the server. You can omit it.
            success: function(data) {
                // This function will run when server returns data
                $('#my-Test').replaceWith(data);
                alert('ok');
                console.log(id);
            },
            error: (error) => {
                console.log('error');
              }
        });
      });
    </script>   

{%endblock%}      

{%block footer%}

{%endblock%}

In views I am getting None value in id

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • 1
    Your template tag should look like this `{% endblock %}` instead of this `{%endblock%}` add space before and after `{%` & `%}` and if you're getting event then get value like this `var id = e.target.vale;` – Ankit Tiwari Aug 20 '22 at 15:50
  • 1
    @AnkitTiwari These are exact same things came into my mind but I didn't comment :) just after 5 minutes when the OP posted question. Also what is `{{text.td}}` inside value attribute? – Sunderam Dubey Aug 20 '22 at 16:06
  • 1
    Hello @SunderamDubey do you have any idea about [this](https://stackoverflow.com/q/73427677/14457833) problem ? I'm facing that problem in my systemc & I think I've to build my python again but I guess there should be better approch – Ankit Tiwari Aug 20 '22 at 16:09
  • Check your browser's network tab! Errors are often returned there. – nigel239 Aug 20 '22 at 20:35
  • @AnkitTiwari that didn't work. again getting None value in ```views.py``` – Sakib-bin-momin Farabi 1911343 Aug 21 '22 at 16:47

0 Answers0