Q:

overwrite HTML object with ajax and javascript

I am working with MySQL data in pyramid, and I need to change an HTML if statement according to the results from JS ajax calls.   
My main page receives data from views.py and passes it on to the .mak script.   The views that matter here are the main view (its got rudimentary authentication) and the view that receives the ajax POST. 
At the moment the main view returns the data object (oData) and a datetime object (history_week) pointing to a week ago. 
views.py:
#This view queries for data that I will use in the mak file

@view_config(route_name='monitor', 
        renderer='monitor:templates/monitor.mak', 
        permission='edit'
        )
def monitor(self):
    import datetime
    request = self.request

    oData = DBSession.query(Alert).order_by(Alert.time)

    history_week = datetime.datetime.now() - datetime.timedelta(days=7)

    return {
        'oData' : oData,
        'logged_in' : request.authenticated_userid,
        'history_week' : history_week,
        }
...
...
#this is the view for my js ajax call

@view_config(route_name="history_ajax",renderer="json")
def history_ajax(self):

    import datetime, json

    iDays = self.request. POST.get('days')

    history_days_ajax = datetime.datetime.now() - datetime.timedelta(days=iDays)

    #because datetime.datetime is not serializable
    from json import JSONEncoder
    class DateEncoder(JSONEncoder):
        def default(self, obj):
            if isinstance(obj, datetime.date):
                return obj.isoformat()
            return JSONEncoder.default(self, obj)

    return {
        'history_days_ajax' : json.dumps(history_days_ajax, cls=DateEncoder),
        }

I use this data to plot data tables, but I only want to plot them to within a certain timeframe.   Currently, all datatables less than a week old get plotted. 
HTML:
% for oAlert in oData[::-1]:
    % if oAlert.time >= history_week:
        <table id="Alerts">
        ...

What I wish to do is an ajax call in javascrip that alters the datetime object such that it points to however many days ago I want.   In other words, 
% if oAlert.time >= history_week should read % if oAlert.time >= history_days_ajax. 
The data ('days') posted in ajax changes according to the user. 
JavaScript:
$(document).ready(function(){
    $('#day').click(function(event){
        $.ajax({
            type: "POST",
            url: "history",
            dataType: "json",
            data: {
                'days': 1,
            },
            async: false,
            success: function(data) {
                history = data.history;
            }, 
        });

Here are my problems:  
1)  How do I change my if statement according to my ajax call with javascript?   Or is there a better way to do this without massively complicating things? 
2)  I need to replace a datetime object in my if statement, since firstly the page needs something to load, and secondly I would like a default of 1 day.   Ideally this should be part of the solution.   
I hope I made everything clear.   It is a rather long question. 

A:

I ended up doing it without ajax and some restructuring:  
Instead of having a static route defined, I now have:
config.add_route(dRoute['monitor'], dRoute['/monitor/{duration}'])

and instead of doing the filtering in HTML, I do it in my views.py via DBSession.   Here I check my url and do my computations in python accordingly, which makes everything a lot easier:
@view_config(route_name='monitor', 
        renderer='monitor:templates/monitor.mak', 
        permission='edit'
        )
def monitor(self):
    import datetime
    request = self.request

    sDuration = request.matchdict['duration']

    if sDuration == 'Day':
        oDateThreshold = datetime.datetime.now() - datetime.timedelta(days=1)
    elif sDuration == 'Week':
        oDateThreshold = datetime.datetime.now() - datetime.timedelta(days=7)
    etc...

    oData = DBSession.query(Alert).order_by(Alert.time).filter(Alert.time>=oDateThreshold)

    return {
        'oData' : oData,
        'logged_in' : request.authenticated_userid,
        'history_week' : history_week,
        'sDuration' : sDuration,
        }

Lastly, I change my path in HTML like this when a button is pressed:
<a id="week" href="${request.route_url('monitor',duration='Week')}">
    1 Week
</a>

Which leaves me free of ajax. 
In words, this is what happens:
When I click on my button, href changes the path of the current page (monitor) to monitor/'duration'.   My views then reads the new url and does its computations depending on it.   The returned data is then used in my .mak file without complicating things there.