I have two views. 1 gets results from search, 2 shows results in html file.
I know I can combine 2 views to 1, after getting results and showing it. I need to separate two function because after getting results from 1, I can show it, using it with pagination and using it with next/back function
I try to use request.session to get data from view 1 to view 2 but It fails
View 1
def list_contract_test(request):
context = {}
if request.method == 'GET':
query1= request.GET.get('search1')
query2= request.GET.get('search2')
submitbutton= request.GET.get('submit')
if query1 is not None:
lookups_contract= Q(contract__icontains=query1)
lookups_name = (Q(name__icontains=query2.upper())|Q(name__icontains=query2.lower()))
contract_posts= Contracts.objects.filter(lookups_contract,lookups_name).distinct()
context['contract_posts'] = contract_posts
request.session['contract_posts'] = contract_posts
return render(request, "customer2.html", context)
else:
contract_posts= Contracts.objects.all()
context['contract_posts'] = contract_posts
return render(request, 'customer2.html', context)
else:
return render(request, 'customer2.html')
View 2
def get_data(request):
contract_posts_test = request.session['results']
context['contract_posts_test'] = contract_posts_test
return render(request, 'customer2.html', context)
In my template customer2.html
{% for contract in contract_posts_test %}
<tr>
<td><a href="{% url 'contract_detail' contract_id=contract.contract %}">{{contract.contract}}</a></td>
<td>{{ contract.name }}</td>
<td>{{ contract.debt }}</td>
<td>{{ contract.created_at}}
</tr>
{% endfor %}
In my url
path("customer2/",get_data,name="get_data"),
The error shows:
...views.py, line 196, in get_data
return render(request, 'customer2.html',results)
else:
return render(request, 'customer2.html')
#request.session['list'] = list_to_process
#list_to_process = request.session['list']
def get_data(request):
contract_posts_test = request.session['results'] … #error this line
#contract_posts_test = val1()
context['contract_posts_test'] = contract_posts_test
return render(request, 'customer2.html', context)
? Local vars
...PythonPython37libsite-packagesdjangocontribsessionsackendsase.py, line 65, in __getitem__
return self._session[key] …
▼ Local vars
Variable Value
key
'results'
self
<django.contrib.sessions.backends.db.SessionStore object at 0x000001E8A2BB8FC8>
Please help me
question from:
https://stackoverflow.com/questions/65888853/get-results-from-one-view-to-another-view-in-django 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…