如何在python django中将模板的上下文变量传递给views.py?

时间:2017-11-06 06:31:04

标签: python django

假设我有这个我的views.py

def exampleclass1(request):
     #do other things

     examplevar = request.POST.getlist('id[]')
     data_dicts = [{'id': id} for id in examplevar]
     for data in data_dicts:
         master_data=ExampleModel.objects.get(id=data.get('id'))
         master_data.save()
     context={
         'sampleid':examplevar,
     }

    #do other things

    return render(request, 'examplepage.html',context)

在我的examplepage.html中,我有这一行:

<a href="{% url 'exampleclass2' sampleid %}"></a>

我的examplesclass2在我的views.py中看起来像这样:

def exampleclass2(request,sampleid):
    examplevar = sampleid
    data_dicts = [{'id': id} for id in examplevar]
    for data in data_dicts:
        master_data=ExampleModel.objects.get(id=data.get('id'))
        master_data.save()

我要做的是通过&#39; sampleid&#39;来自&#39; exampleclass1&#39;的上下文变量到&#39; examplepage&#39;模板,然后传递&#39; sampleid&#39;来自&#39; examplepage&#39;模板到&#39; exampleclass2&#39;。我的urls.py中的urlpattern是什么?我怎样才能做到这一点?

0 个答案:

没有答案
相关问题