본문 바로가기

Django

2022-05-02 개발일기 [Django, Fullcalendar]

반응형

저번에 Django 와 FullCalendar를 database 연동하여 리프레쉬를 해도 일정이 새로고침이 안되는 현상을 해결하고 이번에는 FullCalendar 내장함수인 eventClick 내부에서 Ajax를 사용하여 데이터베이스와 함께 일정을 삭제할 수 있는 스크립트를 완성하였다 ! 애초에 접근방식을 이상하게 하여 시간이 조금 걸렸지만 이렇게 해결할 수 있는 맛에 코딩한다.. 

 

 

이번에는 구글 캘린더 API와 연동을 하니 구글 API 데이터가 Django DB에 데이터셋으로 같이 저장하여 동일한 일정이 2개 발생하고 있는데 이것만 해결하면 캘린더 쪽은 끝난다! 한 걸음만 파이팅! 

 

개발소스[script]

eventClick: function (info) {
    info.jsEvent.stopPropagation();
    info.jsEvent.preventDefault();
    if (confirm("'" + info.event.title + "'을 삭제하시겠씁니까?")) {
        info.event.remove()
    }
    console.log(info.event)

    let title = info.event._def.title;
    let start = info.event._instance.range.start;
    let end = info.event._instance.range.end;
    let tgid = info.event._def.extendedProps.tgid;

    $.ajax({
        url: "{% url 'B2C:etclist' %}",
        method: "POST",
        dataType: "json",
        data: {action: 'test', title: title, start: start, end: end, tgid:tgid}
    })
},

개발소스[view단]

 

def etclist(request):
    calendar_list = Calendar.objects.all()
    x = Calendar.objects.all()

    # delete = request.method
    # if delete.get(''):

    req = request.POST
    if req.get('alldata'):
        testdata = json.loads(req.get('alldata'))
        for i in testdata:

            try:
                test_set = Calendar()
                test_set.title = i['title']
                test_set.start_data = i['start']
                test_set.end_data = i['end']
                test_set.allday = i['allday']
                test_set.save()
            except:
                pass

    result = []
    for iloc in Calendar.objects.all():
        tgdict = dict()
        tgdict['title'] = iloc.title
        tgdict['start'] = iloc.start_data.date().strftime("%Y-%m-%d")
        tgdict['end'] = iloc.end_data.date().strftime("%Y-%m-%d")
        tgdict['tgid'] = iloc.id
        result.append(tgdict)



    context = {
        'calendar': x,
        'calendar_list': calendar_list,
        'test': result
    }

    if req.get('action') =='test':
        tgdata = get_object_or_404(Calendar, pk=req.get('tgid'))
        tgdata.delete()



    return render(request, 'B2C/etclist.html', context)
반응형