我在我的django应用程序中面临一个小问题,无法弄清楚它是什么问题。我有一个帖子请求,该请求已成功从前端发送,Django控制台还列出了URL并给出了状态代码200。但是问题是该视图根本没有触发。
/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>
请让我知道您是否可以解决此问题。我尚未提到添加_task和addaus_task_status视图,但从技术上讲它们的95%相同,并且正在工作。预先感谢。
algorithm_index匹配 everything 的空url ,因此从未调用外部和outier-task-status视图。您应该使用锚:
url('^$', views.algorithm_index, name='algorithm_index'),
或在Django 2.0中使用新的path
语法。