我在Celery任务中处理表单,我可以在HttpResponseRedirect之后渲染它们。
此处在我的views.py中:
from django_celery_results.models import TaskResult
from .forms import SampleForm
from .models import Sample
from analysis.tasks import process_sample_input
def sample_submit(request):
if request.method == "POST":
form = SampleForm(request.POST or None, request.FILES or None)
if form.is_valid():
#form = form.cleaned_data
instance = form.save()
task = process_sample_input.delay(instance.id)
try:
task_result = TaskResult(task_id = task.id)
task_result.save()
return HttpResponseRedirect(reverse("sample:results_detail", kwargs={"id":instance.id, "task_id":task.id}))
except Exception as e:
raise e
#return results_detail(request, instance.id, task.id) #show the results details
else:
print form.errors
return not_processed(request)
else:
form = SampleForm()
context = {
"form": form,
}
return render(request, "analysis/sample_form.html", context)
我的问题是如何从结果列表页面访问results_detail呈现的html。我不知道如何从其他地方访问我的Celery任务ID。 sample_submit()
在我的views.py中:
def results_list(request):
all_samples = Sample.objects.all()
context = {'all_samples': all_samples}
return render(request, "analysis/results_list.html", context)
在我的results_list.html
中{% block content %}
<table>
<thead>
<tr>
<th>Name [Local File or URL]</th>
</tr>
</thead>
{% for sample in all_samples %}
<td><a href='/sample/results/{{sample.id}}/'>{{ sample.get_sample_name }}</a></td>
{% endfor %}
</table>
{% endblock %}
在myapp / analysis / urls.py中:
url(r'^results/(?P<id>[0-9]+)/$', views.results_detail, name="results_detail"),
在urls.py
和模板中,我只能传递一个参数。如果有办法,如何将Celery taskresult id(自动生成)传递给视图?任何帮助或想法?
答案 0 :(得分:0)
由于很难传递两个参数,我只是决定让我的应用模型ID与Enumerable.Range(0, words.Length - 1).Select(i => words[i] + ' ' + words[i + 1]).ToArray();
相同。因此,在视图中访问更容易:
taskresult.id