【问题标题】:Wrong redirection of url in djangodjango中的url重定向错误
【发布时间】:2020-06-17 11:11:36
【问题描述】:

更新问题:

Django 中的 URL 重定向错误。我有这个:

views.py.

def graph(request):
    if request.method == 'POST' and 'text' in request.POST:
        print("testing....")
        print(request.POST.get('text'))
        name = request.POST.get('text')
        context = {
            'name': name,
        }
        print(context)
        return render(request, 'StockPrediction/chart.html', context)
    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'),
]

出于测试目的,我正在使用打印语句。所以在打印print(context) 之前没有问题,但问题是它转到'StockPrediction/greet.html' 而不是'StockPrediction/chart.html'。我需要的。

【问题讨论】:

  • 删除company's symbol 中的's 以改进格式。
  • 为什么request.POST 的键中应该有_graph
  • 那应该是什么??
  • 什么都没有。 if request.method == 'POST' 就够了。或and request.is_ajax()
  • '_graph' in request.POST: to 因为我有两个提交按钮。

标签: python jquery django ajax django-views


【解决方案1】:

你应该使用ajax请求:

$.ajax({
    type: 'POST',
    url: 'YOUR VIEW URL',
    data: {'row': row, 'text': text},
    success: function (data){
        DO SOMETHING HERE if VIEW has no errors
    })

在你看来:

row = request.POST.get('row')    
text = request.POST.get('text')

你也应该关心 crsf-token。 Documentation

【讨论】:

  • 嘿,我已经更新了这个问题。可以调查一下。
  • 将 if request.method == 'POST' 替换为 if request.is_ajax()。您在浏览器控制台或 django 错误中是否有任何错误?
  • 不,但它进入else 条件
  • 请帮帮我。
  • 嘿,我可以解决问题,但是 URL 重定向错误。我已经更新了问题,请帮忙。
【解决方案2】:

你可以POSTGET它或者把它作为一个变量放在你的url中。这是一个post方法:

使用jquery

$.ajax({
    url : "/URL/to/view", 
    type : "POST", // or GET depends on you
    data : { text: $text },
    async: false,
    // handle a successful response
    success : function(json) {
         // some code to do with response
         }
    },

    // handle a non-successful response
    error : function(xhr,errmsg,err) {
        $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
            " <a href='#' class='close'>&times;</a></div>"); // add the error to the dom
        console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
    }
});

在您看来,您可以将数据作为 json 获取并返回 josn 作为响应

import json
def my_view(request):
    if request.method == 'POST':
         response_data = {}   // to return something as json response
         text = request.POST['text']
         ...
         return HttpResponse(
         json.dumps(response_data),
         content_type="application/json"
    else:
        return HttpResponse(
            json.dumps({"nothing to see": "this isn't happening"}),
            content_type="application/json"
        )

【讨论】:

  • 嘿,我已经更新了这个问题。可以调查一下。
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 2021-06-16
  • 2018-01-06
  • 1970-01-01
  • 2021-10-29
  • 2012-11-25
  • 2012-10-03
  • 1970-01-01
相关资源
最近更新 更多