pissall
pissall

Reputation: 7399

Django: POST request successful but nothing on backend

I am facing a small problem in my Django app and not able to figure out what is wrong with it. I have a POST request that is getting sent successfully from the front-end, the django console also hits the URL and gives a status code 200. But the problem is that the view is not getting triggered at all.

/urls.py

urlpatterns = [
    url('addition/', views.addition_task, name='addition'),
    url('addition-task-status/', views.addition_task_status, name='addition_task_status'),
    url('', views.algorithm_index, name='algorithm_index'),
    url('outlier/', views.run_outlier_task, name='run_outlier'),
    url('outlier-task-status/', views.outlier_task_status, name='outlier_task_status'),
    ]

/views.py

@csrf_exempt
def run_outlier_task(request):
    print("I'm here")

    if request.method == "POST":
        print("Request is post")
        metric = request.POST["metric_variable"]
        print(metric)
        path = ['MKT', 'CP_MANUFACTURER', 'CP_FRANCHISE', 'CP_BRAND', 'CP_SUBBRAND']
        drivers = ['Cumulative_Distribution_Pts', 'pct_Stores_Selling', 'Baseline_RASP_per_EQ']

        if request.session.get('file_path', None) == None:
            file_name = "anon_cntr_out_br.csv"
        else:
            file_name = request.session.get('file_path', None)

        outlier_task = outlier_algorithm.delay(path, metric, file_name, drivers)
        return HttpResponseRedirect(reverse("outlier_task_status") + "?job_id=" + outlier_task.id)
    else:
        return HttpResponse("GET Request")

def outlier_task_status(request):
    if 'job_id' in request.GET:
        job_id = request.GET['job_id']
        job = AsyncResult(job_id)
        data = job._get_task_meta()
        return HttpResponse(json.dumps(data['result']))
    else:
        HttpResponse("No job ID given")

/templates/algorithm.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Algorithms</title>
</head>
<body>
<div>
    <form action="{% url 'run_outlier' %}" method="post">
        {% csrf_token %}
        <input type="text" name="metric_variable">
        <input type="submit" value="Run algo with default file and attributes" />
    </form>
</div>
</body>
</html>

Please let me know if you can fix this problem. I have not mentioned the addition_task and addition_task_status views, but they are technically 95% same and they are working. Thanks in advance.

Upvotes: 0

Views: 227

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599580

Your empty URL for algorithm_index matches everything, so the outlier and outlier-task-status views are never called. You should use anchors:

url('^$', views.algorithm_index, name='algorithm_index'),

or, use the new path syntax in Django 2.0.

Upvotes: 7

Related Questions