Skip to main content
 首页 » 编程设计

python之如何预约并将其保存到数据库 Flask

2025年02月15日9youxin

我正在制作一个小应用程序,客户可以在其中在日历中进行约会,但我不知道如何将约会保存在数据库中,然后按日期显示给他们。

完整编辑:

我创建了带有一些垃圾约会的日历,如下所示:

我想将约会保存在数据库中,因此当任何客户端在提交后点击任何日期时,ajax 请求将发送到我可以处理请求的 View ,这里是 view.py :

@abonent_panel_route.route('/panel/make/appointment/') 
@abonent_panel_route.route('/panel/make/appointment/', methods=['GET', 'POST']) 
def get_appo(): 
    # user = User.query.filter_by(id=current_user.id).first_or_404() 
 
    import json 
    getData = str(request.args.getlist('data')) 
    data  = json.loads(getData) 
 
    if data : 
        print "\n\n\n\n" + str(data['title']) + "\n\n\n\n" 
        return jsonify({'success': data}) 
    else: 
        print "\n\n\n\n" + str(len(data)) + "\n\n\n\n" 
        return jsonify({'error': 'No data!'}) 

这里是 javascript 代码:

$(document).ready(function() { 
    var initialLocaleCode = 'ru'; 
    var d = new Date(); 
    var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate(); 
 
        $('#calendar').fullCalendar({ 
        header: { 
            left: 'prev,next today', 
            center: 'title', 
            right: 'month,agendaWeek,agendaDay,listMonth' 
        }, 
        defaultDate: strDate, 
        height: 650, 
        locale: initialLocaleCode, 
        navLinks: true, // can click day/week names to navigate views 
        selectable: true, 
        selectHelper: true, 
        select: function(start, end) { 
            var title = prompt('Event Title:'); 
            var eventData; 
            if (title) { 
                eventData = { 
                    title: title, 
                    start: start, 
                    end: end 
                }; 
                $('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true 
                $(this).css({'background-color':'red'}); 
                $.ajax({ 
                    type: 'POST', 
                    url: '{{url_for("panel_abonent.get_appo")}}', 
                    data: [eventData] 
                }) 
                .done(function(data){ 
 
                    if(data.error){ 
                        console.log(eventData); 
                    } 
                    else { 
                        console.log(eventData); 
                    } 
 
                }); 
            } 
            $('#calendar').fullCalendar('unselect'); 
        }, 
 
 
        editable: true, 
        eventLimit: true, 
        events: [ 
            { 
                title: 'All Day Event', 
                start: strDate 
            }, 
            { 
                title: 'Long Event', 
                start: '2017-04-18', 
                end: '2017-04-20' 
            }, 
            { 
                id: 999, 
                title: 'Repeating Event', 
                start: '2017-04-20' 
            }, 
            { 
                id: 999, 
                title: 'Repeating Event', 
                start: '2017-04-29' 
            }, 
        ] 
    }); 
 
    $.each($.fullCalendar.locales, function(localeCode) { 
        $('#locale-selector').append( 
            $('<option/>') 
                .attr('value', localeCode) 
                .prop('selected', localeCode == initialLocaleCode) 
                .text(localeCode) 
        ); 
    }); 
 
    $('#locale-selector').on('change', function() { 
        if (this.value) { 
            $('#calendar').fullCalendar('option', 'locale', this.value); 
        } 
    }); 
}); 

在控制台中,我可以看到所选日期的数据列表,如标题、开始和结束时间,这里是:

Object {title: "asfawg", start: r, end: r} 
start : r{ 
    "_ambigTime":true, 
    "_ambigZone":true, 
    "_d" : "Thu Apr 06 2017 06:00:00 GMT+0600 (+06)", 
    "_fullCalendar":true, 
    "_i":"2017/4/19", 
    "_isAMomentObject":true, 
    "_isUTC":true, 
    "_isValid":true, 
    "_locale":e, 
    "_offset":0, 
    "_pf":Object, 
    "__proto__":Object, 
    "title":"asfawg" 
} 

在 view.py 中,我已将字典转换为列表以便于从列表中获取数据,因此您有一个键和一个值,如您所见在我打印标题的地方 print "\n\n\n\n"+ str(data['title']) + "\n\n\n\n" 我应该得到值标题是 asfawg,但我什么也没得到,请求总是返回 No data. 并且列表的 length 返回 0。

拜托,任何人都可以告诉我我犯的错误在哪里。

请您参考如下方法:

我解决了这个问题,eventData 本身是一个字典而不是一个列表。

因此,如果我想在 ajax 请求中获取开始日期,然后将其发送到我的 view.py,我会做类似的事情:

data: { 
    'data': title, 
    'start': eventData['start']['_d'], 
    'end': eventData['end']['_d'] 
} 

我稍微改变了我的观点,我添加了一个函数,它接受任何 json 参数并将其作为 json 字典返回,我使用 flask_restful 来完成这项工作,这是函数:

def parse_arg_from_requests(arg, **kwargs): 
 
    from flask_restful import reqparse 
    parse = reqparse.RequestParser() 
    parse.add_argument(arg, **kwargs) 
    args = parse.parse_args() 
    return args[arg] 

接下来就很简单了,按照下面的顺序一个一个获取参数即可:

data = parse_arg_from_requests('data') 
start = parse_arg_from_requests('start') 
end = parse_arg_from_requests('end') 
return render_template('panel/panel.html')