Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Ajax在django中返回'none'_Python_Jquery_Django_Ajax_Django Views - Fatal编程技术网

Python Ajax在django中返回'none'

Python Ajax在django中返回'none',python,jquery,django,ajax,django-views,Python,Jquery,Django,Ajax,Django Views,我把这个表单放在一个有两个按钮的表中。 代码: index.html <form action="{% url 'Prediction' %}" method="post"> {% csrf_token %} <table class="table table-striped table-dark" cellspacing="0"> <thead class="bg-info"> <tr>

我把这个表单放在一个有两个
按钮的表中。
代码:
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>
但问题是在
views.py
中返回
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')
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'),
]
url.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')
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'),
]

尝试向请求添加数据类型:“json”。例如:

$.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)
    }
});

尝试向请求添加数据类型:“json”。例如:

$.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)
    }
});

在视图函数图(请求)中,您没有返回值,而是使用请求呈现HTML页面

Django中的视图函数应该返回JSON或字典,或者可以返回网页,您可以执行以下操作之一

1) 返回字典或JSON

return {"key":"value","key":"value"}
2) 返回一个HttpResponse

return HttpResponse(status_code, response_data)
3) 重定向到另一页

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

请尝试上面的方法,如果有帮助,请进行注释。

在视图函数图(请求)中,您没有返回值,而是使用请求呈现HTML页面

Django中的视图函数应该返回JSON或字典,或者可以返回网页,您可以执行以下操作之一

1) 返回字典或JSON

return {"key":"value","key":"value"}
2) 返回一个HttpResponse

return HttpResponse(status_code, response_data)
3) 重定向到另一页

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

请尝试以上内容,如果有帮助,请发表评论

您收到的错误消息是什么?没有错误
request.POST.get('text')
正在返回
none
。console.log($text)是否返回正确的值?
console.log($text)
。是的,它返回正确的值。请帮助我。您收到的错误消息是什么?没有错误
request.POST.get('text')
正在返回
none
。console.log($text)是否返回正确的值?
console.log($text)
。是的,它返回正确的值。请帮助我。仍然是一样的。我认为问题可能是jquery没有传递值
text
,或者ajax没有在
views.py
中传递给Django。这种方法正确吗?您说console.log($text)包含值,所以这应该是方法。请帮助我。还是一样。我认为问题可能是jquery没有传递值
text
,或者ajax没有在
views.py
中传递给Django。这是正确的方法吗?你说console.log($text)包含值,所以这应该是方法。请帮助我。