Karan Gupta
Karan Gupta

Reputation: 13

Ajax returning `none` in django

I Have this form in a table with two button. Code: index.html

<form action="{% url 'Prediction' %}" method="post">
    {% csrf_token %}
    <table class="table table-striped table-dark" cellspacing="0">
        <thead class="bg-info">
            <tr>
               <th>Company's Symbol</th>
               <th>Current Price</th>
               <th>View Current chart</th>
               <th>Action</th>
            </tr>
        </thead>
        <tbody>
           {% for a,b in stocks %}
               <tr>
                  <th scope="row" class="comp_name">{{ a }}</th>
                  <td>{{ b }}</td>
                  <td>
                     <input type="submit" class="btn graph-btn" formaction="{% url 'Graph' %}" name="_graph" value="View Graph">
                  </td>
                  <td>
                     <input type="submit" class="btn predict-btn" name="_predict" value="Predict Closing Price">
                  </td>
              </tr>
           {% endfor %}
        </tbody>
    </table>
</form>

By clicking .graph-btn I need to pass the data of row that button is clicked on. Here is code that's working.

<script>
    $(".graph-btn").click(function() {
    var $row = $(this).closest("tr");
    var $text = $row.find(".comp_name").text();
    console.log($text);
    $.ajax({
        type:'POST',
        url:'{% url 'Graph' %}',
        data:{
            text: $text,
            csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
        },
        success:function(json){
        console.log($text);
        },
        error : function(xhr,errmsg,err) {
        }
    });
});
</script>

But Problem is that In views.py returning none:

views.py

def graph(request):
    if request.method == 'POST' and '_graph' in request.POST:
        print(request.POST.get('text'))
        # context = {'name': name}
        # # print(name)
        return render(request, 'StockPrediction/chart.html')
    else:
        return render(request, 'StockPrediction/greet.html')

urls.py

urlpatterns = [
    path("", views.greet, name='greet'),
    path("index/", views.index, name='Stock Prediction'),
    path("prediction/", views.prediction, name='Prediction'),
    path("view/", views.graph, name='Graph'),
]

Upvotes: 1

Views: 909

Answers (2)

user7552123
user7552123

Reputation:

In your views function graph(request) you are not returning a value but instead rendering an HTML page with the request.

A view function in Django should either return a JSON or a dictionary or can return a Webpage You can either do one of following

1) return a dictionary or JSON

return {"key":"value","key":"value"}

2) return a HttpResponse

return HttpResponse(status_code, response_data)

3) Redirect to another page

return redirect('/some/redirect/url/')

Try the above and comment if it helps

Upvotes: 0

Vincent
Vincent

Reputation: 1564

try adding dataType: 'json' to the request. For example:

$.ajax({
    type: 'post',
    url: '{% url 'Graph' %}',
    headers: {
        'X-CSRFToken': $('input[name=csrfmiddlewaretoken]').val()
    }
    data: {
        text: $text,
    },
    dataType: 'json',
    success: function (data) {
       console.log(data)
    },
    error: function(error) {
       console.log(error)
    }
});

Upvotes: 1

Related Questions