简体   繁体   English

在基于Django类的视图中避免重复

[英]Avoiding repetition in Django class-based view

I am writing a web application in Django, which is used in a particular way. 我正在用Django编写Web应用程序,该应用程序以特定方式使用。 Instead of using models stored in a database, it builds forms dynamically from JSON data gathered from another application and platform using REST API. 它不是使用存储在数据库中的模型,而是使用REST API根据从另一个应用程序和平台收集的JSON数据动态构建表单。

The web page renders a form, which displays a list of mathematical parameters and their values. 该网页呈现一个表单,其中显示了数学参数及其值的列表。 The user can then change (or not) those values and press a "Run" button to display some calculation results. 然后,用户可以更改(或不更改)这些值,然后按“运行”按钮以显示一些计算结果。

The forms are built from data obtained by querying JSON data via a URL (which gives me the list of parameters and their initial values). 表单是根据通过URL查询JSON数据而获得的数据构建的(URL给了我参数列表及其初始值)。 By specification, I have to use Django and do not use the database to store any parameter value data (the only data which are stored are the URL addresses of the JSON data). 根据规范,我必须使用Django,而不必使用数据库来存储任何参数值数据(唯一存储的数据是JSON数据的URL地址)。

I ended up with some working solution, using CBV. 我最终使用CBV找到了一些可行的解决方案。 I have a DetailedView, of that structure: 我有一个详细的结构视图:

class SimulationView(DetailView):
    template_name='template.html'
    model=SimModel # provides URLs for REST API (URLs for querying parameter list and simulation function)

    # this is used to display the page with GET
    def get_context_data(self, **kwargs):
        # conn.request function that returns param_JSON in JSON/REST
        # for a SUBSET of parameters in param_JSON build a list of entries named init_entries. Note not all parameters from the JSON request are used for the user interface.
        # form = paramForm(initial=init_entries) and store in context['form']
        return context

    def post(self, request, *args, **kwargs):
        # because the user may have changed parameter values, need to rebuild the JSON dataset to return to the URL with a simulation request
        # conn.request function that returns param_list in JSON/REST
        # for each param in JSON param_list build a list of entries
        # form = paramForm(request.POST, request.FILES, initial=init_entries) and store in context['form']
        # use form data to build REST request for the simulation function
        # conn.request simulation function and get result in JSON
        # store result in context['result']
        return render(request, 'template.html', context)

template.html is reponsible to display both the initial form when doing a GET, and the results as well when doing a POST. template.html负责在执行GET时显示初始表单,并在执行POST时显示结果。

As you can see, there is a performance issue. 如您所见,存在性能问题。 When you do GET to build the page, you have to do the REST connection to get the data and build the form and interface (this is normal). 当执行GET来构建页面时,必须进行REST连接以获取数据并构建表单和界面(这是正常的)。 But when you POST to request simulation, you need to run the URL connection again to get the parameter list in JSON format, change the values, and then request simulation results. 但是,在POST请求模拟时,您需要再次运行URL连接以JSON格式获取参数列表,更改值,然后请求模拟结果。 Please note that the REST request returns many more parameters than the one displayed to the user, hence it is not possible to use only the form data to build a proper JSON request. 请注意,REST请求返回的参数比向用户显示的参数多得多,因此无法仅使用表单数据来构建适当的JSON请求。 This works, but is inefficient. 这可行,但是效率低下。 I tried to store param_JSON in a field of the class, but this does not work: the class is instanciated again when doing a POST, and param_JSON values are lost. 我试图将param_JSON存储在类的字段中,但这不起作用:在执行POST时再次实例化该类,并且param_JSON值丢失。

Do I need a get function? 我需要获取功能吗? Or am I doing it totally wrong? 还是我做错了? In general, is there a better way? 总的来说,有没有更好的方法? Thanks a lot in adavance for your suggestions. 非常感谢您的建议。

Class based views are specifically designed to prevent you storing things in instance attributes, since this is not thread-safe; 基于类的视图是专门为防止您将实例存储在实例属性中而设计的,因为它不是线程安全的。 data would be shared between all requests. 数据将在所有请求之间共享。

The place to store data between separate requests is the session . 在单独的请求之间存储数据的地方是会话

I am going back to my original question. 我要回到最初的问题。 I was wondering if the best solution was to use AJAX? 我想知道最好的解决方案是使用AJAX吗? I render the form once when user accesses the page via GET, and then POST calls are processed with AJAX, which only updates the result part of the page (graphs, table) without rendering the form again. 当用户通过GET访问页面时,我会渲染一次表单,然后使用AJAX处理POST调用,这只会更新页面的结果部分(图形,表格),而无需再次渲染表单。 Does that sound a reasonable solution? 这听起来合理吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM