如何在django视图之间传递数据

时间:2022-03-14 18:28:41

This questions addresses my question genearally, but I am looking for a more specific explanation.

这个问题基本上解决了我的问题,但我正在寻找更具体的解释。

I would like a user to update a a group of model objects, however, the queryset for these objects will need to be retrieved first. My plan is to do this in two seperate URs/views, getting the query set info from the first, then displaying the model formset to be updated next.

我希望用户更新一组模型对象,但是,需要首先检索这些对象的查询集。我的计划是在两个单独的UR /视图中执行此操作,从第一个获取查询集信息,然后显示下一个要更新的模型表单集。

My first view gives a list of all the the "Project"s (One of my models), and retrieves the id of the project selected.

我的第一个视图给出了所有“项目”(我的一个模型)的列表,并检索所选项目的ID。

Here is the form:

这是表格:

class ProjectLookupForm(forms.Form):
    Project_Name = chosenforms.ChosenModelChoiceField(queryset=Project.objects.all())

and here is the view:

这是观点:

def update_project_filter(request):
    project_form = ProjectLookupForm(request.POST or None)
    if request.method == 'POST':
        if project_form.is_valid():
            context = {"project_form":project_form}
            # Get project here and share it with the next view.
            selected_project_id = project_form.cleaned_data["Project_Name"].id
            # Add a new return statement here?
            # Or call update project view from here?
            # Add a redirect button to html?
        else:
            errors = project_form.errors
            context = {"errors":errors, "project_form":project_form}
    else:
        context = {"project_form":project_form}
    return render(request, 'filter_update_project_form.html', context)

As one can see, I have included some comments brainstorming what my possibilities are. My goal is to send the selected_project_id to this next view, so that it can use that id as a model form query set.

可以看出,我已经包含了一些头脑风暴我的可能性的评论。我的目标是将selected_project_id发送到下一个视图,以便它可以将该id用作模型表单查询集。

def update_project(request):
    UpdateFormset = modelformset_factory(Sample, fields=("sample_name", "extraction_date", 
                                                     "project", "order", "notebook", "notebook_page"))
    if request.method == 'POST':
        formset = UpdateFormset(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            context = {"formset": formset, "project_form":project_form}
        else:
            errors = formset.errors
            context = {"formset":formset, "errors":errors, "project_form":project_form}
    else:
        formset = UpdateFormset(queryset=Sample.objects.filter(project=2))
        context = {"formset":formset, "project_form":project_form}
    return render(request, 'update_project_form.html', context)

One can see here that I have hard coded the queryset like so:

我可以在这里看到我对查询集进行了硬编码,如下所示:

queryset=Sample.objects.filter(project=2)

How can I set "project=" to my selected_project_id? Do I pass this info to the view as an input parameter? Or do I send it to the next URL and take it from there?

如何将“project =”设置为selected_project_id?我是否将此信息作为输入参数传递给视图?或者我将它发送到下一个URL并从那里获取它?

1 个解决方案

#1


13  

Assuming you've activated django.contrib.sessions.middleware.SessionMiddleware; you can pass data between views using request.session dictionary as follows:

假设你已经激活了django.contrib.sessions.middleware.SessionMiddleware;您可以使用request.session字典在视图之间传递数据,如下所示:

def update_project_filter(request):
    ...
    selected_project_id = project_form.cleaned_data["Project_Name"].id
    request.session['selected_project_id'] = selected_project_id
    ...

def update_project(request):
    ...
    selected_project_id = request.session.get('selected_project_id')
    ...

#1


13  

Assuming you've activated django.contrib.sessions.middleware.SessionMiddleware; you can pass data between views using request.session dictionary as follows:

假设你已经激活了django.contrib.sessions.middleware.SessionMiddleware;您可以使用request.session字典在视图之间传递数据,如下所示:

def update_project_filter(request):
    ...
    selected_project_id = project_form.cleaned_data["Project_Name"].id
    request.session['selected_project_id'] = selected_project_id
    ...

def update_project(request):
    ...
    selected_project_id = request.session.get('selected_project_id')
    ...