Django - 如何在与views.py不同的模块中调用会话?

时间:2017-11-24 00:39:47

标签: django django-sessions

我想在我的forms.py中调用在views.py中设置的会话。这可能吗?

我尝试这样做,但我遇到了一些问题,请求'。

forms.py

from django import forms


class NormalForm(forms.Form):

   options = [
       ('TEST','TEST'),
   ]

   options_normalform = forms.ChoiceField(required=False, choices=options,widget=forms.RadioSelect(attrs={'class':'example_form'}), label='')

class ExampleForm(forms.Form):

   def __init__(self, *args, **kwargs):
       self.request = kwargs.pop('request', None)
       super(ExampleForm, self).__init__(*args, **kwargs)

       self.option_a = self.request.session['my_session']

    self.options_list = [
        ('a', self.option_a),
        ('b', 'b'),
        ('c', 'c'),
    ]

    self.options_form = forms.ChoiceField(required=False, choices=self.options_list,widget=forms.RadioSelect(attrs={'class':'example_form'}), label='')

views.py

from django.shortcuts import render
from .forms import ExampleForm, NormalForm

def example(request):
    option_a = '123'
    request.session['my_session'] = option_a

    normalform = NormalForm
    form = ExampleForm(request=request)
    context = {
        'form': form,
        'normalform': normalform,
    }

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

模板

<!DOCTYPE html>
<html>
    <head>
        <title>My App</title>
    </head>
    <body>       
        <!-- Header -->
        <header>
            <h1 id="logo">MY APP</h1>
        </header>


        <!-- Options form -->
        <div id="forms">
            <form id="normalform" method="post">
                {% csrf_token %}
                {{normalform}}
            </form>
            <form id="form" method="post">
                {% csrf_token %}
                {{form}}
            </form>
        </div>
        <!-- End of options form -->


        <!-- Footer -->
        <footer id="main-footer">
            <p>Copyright &copy; 2017-2017 | my APP </p>
        </footer>
        <!-- End of Footer -->
    </body>
</html>

我没有尝试导入值而不是创建会话,因为这个值将在我调用表单的同一视图中创建。因此我会得到一个循环错误。

我希望将此值存储在会话中,然后将其发送到我的表单中不会引发此循环问题。

P.S。我是编程的初学者。

[已编辑]包含模板和新表单以测试视图是否能够找到模板。

1 个答案:

答案 0 :(得分:1)

首先,我们重写表单类,以便它在构造函数中接收请求对象并将其存储在字段中。之后所有其他属性都将被初始化:

class ExampleForm(forms.Form):

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(ExampleForm, self).__init__(*args, **kwargs)

        self.option_a = self.request.session['my_session']

        self.options_list = [
            ('a', self.option_a),
            ('b', 'b'),
            ('c', 'c'),
        ]

        self.fields['options_form'] = forms.ChoiceField(required=False, choices=self.options_list, widget=forms.RadioSelect(attrs={'class':'example_form'}), label='')

现在,在您的视图中,您必须实例化一个表单对象并在构造函数调用中传递请求。请注意,上下文应该接收表单实例而不是类,否则表单将不会呈现:

def example(request):
    option_a = '123'
    request.session['my_session'] = option_a
    form = ExampleForm(request=request)
    context = {
        'form': form,
    }
    return render(request, 'example/index.html', context)