0

I am processing a form in my Celery task and I can already render them after HttpResponseRedirect.

Here in my 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)

My problem is how can I access the html rendered by results_detail from a results list page. I don't know how I can access my Celery task ID elsewhere from the views' sample_submit()

In my views.py:

def results_list(request):
    all_samples = Sample.objects.all()
    context = {'all_samples': all_samples}  
    return render(request, "analysis/results_list.html", context)

In my 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 %}

in the myapp/analysis/urls.py:

   url(r'^results/(?P<id>[0-9]+)/$', views.results_detail, name="results_detail"),

Here in urls.py and templates, I can pass only one parameter. How can I pass the Celery taskresult id (auto-generated) to the views if there is a way? Any help or ideas?

1 Answer 1

0

Since it was difficult to pass two parameters, I just settled on having my app model's id same with taskresult.id. So that it is easier to access in the views:

def results_detail(request, id=1): 
    sample_instance = get_object_or_404(Sample, id = id)
    result_obj = None
    try:
        result_obj = TaskResult.objects.get(id=id)
    except Exception as e:
        raise e
    try:
        #AsyncResult waits for the Celery task results to finish and return results
        taskresult = AsyncResult(result_obj.task_id)
        # --- Accessing AsyncResult's results for Thug logs ---
        #ast.literal_eval for reading the list in a string
        if taskresult.get():
             #did this to make sure results are already available. Manually asking django-celery-taskresult's object.status returns PENDING. taskresult.get() will wait for the task.
             # include next code here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.