简体   繁体   中英

How to maintain form parameters when new url is called

I have a form shown in the first picture below and a set of navpills shown in the second picture. When I click the navpill it calls a url that either ends with 'charts' or 'tables'. Whenever I press the navpill though all the values in the form are cleared to the original template. I need them to stay so when I switch from charts to tables I get the same information. ie if there is a fund type selected I want it to still be their if I click on tables when I was previously on charts. I tried using "GET" as such:

 min_year = request.GET.get('year_min', None)
if not min_year:
    min_year = '2013'

But that only works if I submit the form. As soon as I click on one of the values it resets.

Here is the html for the "Start Year" box:

<h6>Start Year</h6>
            <div class="form-group">
                {{form.year_min|attr:"class:form-control"}}
            </div>

And here is the form:

    CHOICES = (
    ('1998', '1998'),
    ('1999', '1999'),
    ('2001', '2001'),
    ('2002', '2002'),
    ('2003', '2003'),
    ('2004', '2004'),
    ('2005', '2005'),
    ('2006', '2006'),
    ('2007', '2007'),
    ('2008', '2008'),
    ('2009', '2009'),
    ('2010', '2010'),
    ('2011', '2011'),
    ('2012', '2012'),
    ('2013', '2013'),
    ('2014', '2014'),
    ('2015', '2015'),
    ('2016', '2016'),
    ('2017', '2017'),
    ('2018', '2018'),
)

year_min = forms.ChoiceField(choices=CHOICES, required=True, label='Start Year')

Pictures: 在此处输入图片说明 在此处输入图片说明

Looks like you would need to use Ajax. When you do a GET by changing the URL of your page (or by clicking on a normal link), you are essentially navigating away from that page. There should be ways to preserve the form data even in that case, but I'd recommend that you take a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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