Skip to content
Advertisement

Getting fullcalendar javascript click data to django modelform

i want to click event on Fullcalendar, then it open another page, like this

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var calendarEl = document.getElementById('calendar');
        var calendar = new FullCalendar.Calendar(calendarEl, {
            dateClick: function(info) {
                window.open("{% url 'create-book' %}","_self");
            },

. but how to get data from fullcalendar ? there are function dateStr for show date, and resource.id, to show resource in timeline view(fullcalendar)

 alert('clicked ' + info.dateStr + ' on resource ' + info.resource.id);           

. what i want is bring dateStr and resource.id clicked data to django modelform,

here views.py

@login_required(login_url='login')
def create_book(request):
    booking_forms = BookingForm(initial={
        'resourceId':'xxxx',
        'startdate':'xxxx'
    })

im not sure what should i put on xxxx to get this through url on other page..

Thanks..

Advertisement

Answer

In any web application, if you want to pass data to another page, you generally add it to the URL in some way.

e.g. something like /create_book?resourceId=123&startdate=2021-02-18.

So in your case I’d expect something like

window.open("{% url 'create-book' %}?resourceId=" + encodeURIComponent(info.resource.id) + "&startdate=" + encodeURIComponent(info.dateStr),"_self");

would be needed on the client-side, and

@login_required(login_url='login')
def create_book(request):
    booking_forms = BookingForm(initial={
        'resourceId': request.GET.get('resourceId'),
        'startdate': request.GET.get('startdate')
    })

on the server side.

Try it and let me know. (I’m not a django or python user, so the server-side code I’m not 100% about, but from reading this and this I think I’ve got the idea.)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement