简体   繁体   English

django 中的 url 重定向错误

[英]Wrong redirection of url in django

Updated Question:更新的问题:

Wrong redirection of URL in Django. Django 中的 URL 重定向错误。 I have this:我有这个:

views.py . 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'),
]

for testing purposes, I m using a print statement.出于测试目的,我正在使用打印语句。 So there is no problem until printing print(context) but the problem is it goes to 'StockPrediction/greet.html' not 'StockPrediction/chart.html' .所以在打印print(context)之前没有问题,但问题是它转到'StockPrediction/greet.html'而不是'StockPrediction/chart.html' which I need.我需要的。

You should use ajax request:您应该使用 ajax 请求:

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

in your view:在你看来:

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

also you should care about crsf-token.你也应该关心crsf-token。 Documentation 文档

your can POST it GET it or put it as a variable in your url .您可以POST GET或将其作为变量放入url中。 here is a post approach:这是一个帖子方法:

using jquery :使用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
    }
});

In your view you can get the data as json and return josn as response在您看来,您可以将数据作为 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"
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM