0

my views.py

from django.shortcuts import render
from django.http import JsonResponse
from home_page.models import UploadNet
from home_page.forms import NetForm, ConfigForm
from backend.NetListBuilder import NetListBuilder
from backend.ConfigBuilder import ConfigBuilder

def set_configuration(request, net_file=None):
    if "upload_net" in request.POST:
        net_path = os.path.join("media/net_files", net_file.name)
        netlist = NetListBuilder(net_path)
        config = ConfigBuilder(netlist)
        context = {'config': config,}
        return render(request,'configuration.html', context=context,)
    return render(request,'configuration.html', {'net_file': None},)

def save_config(request):
    if request.is_ajax():
        response_data = {}
        json_data = {}
        json_data = request.POST['json_data']:
        response_data["result"] = "save file" 
        return JsonResponse(response_data)
    context = {'net_file': None}
    return render(request, 'rules_list.html', context=context)

I want to share netlist and config objects (without saving them in DB) in rules_list view. how can i do that? should i pass them into configuration.html file?

1 Answers1

0

Django is built on the philosophy of request->process->response. Hence there cannot be shared states between views. But in case you still do want to share, then either use, DB, the browser's local storage or session available in Django. https://docs.djangoproject.com/en/3.0/topics/http/sessions/

If you are trying to share a query output or nonserializable data, then you would have to use a cache. https://docs.djangoproject.com/en/3.0/topics/cache/
OR

IF not cache then a temporary table, where you could dump data and clear the data after you have finished using.

Harsh Nagarkar
  • 697
  • 7
  • 23
  • ok. is it right to save it in the session? if so, should i write a serializer? – ריקי נוישטט Apr 06 '20 at 07:58
  • i couldn't find reference how to build a serialize for external object. – ריקי נוישטט Apr 06 '20 at 08:39
  • I am not exactly sure as to what you are trying to store. What is inside NetListBuilder. Also if your data is not serializable then you would have to use a temporary database table and delete entry after use or use cache. https://stackoverflow.com/questions/4631865/caching-query-results-in-django – Harsh Nagarkar Apr 07 '20 at 13:51