Save django variable using ajax

176 views Asked by At

I'm trying to do a multistep form using Django 3. The context of this form it is to make a schedule for a exam. The user shall choose a day and time. But when day is chosen, a filter is applied to show the number of vacant seats. Here starts the problem. The way I've been planed to make this possible is using AJAX to collect a value from DATE option and send to back-end to filter. But, the value on back-end is not changing. Here is the code:

model are: Day, Time and scheduling(day and time foreignkey)

   {% extends "base_restrictedArea.html" %}
   {% load filter_agenda %}

   {% block main%}
   <div class="container">
     <div class="row">
        <h2>Sscheduling</h2>
        
            <form id="multistep_form" action="{% url 'my_url' %}" method="post">
                {% csrf_token %}
                <fieldset>
                    {% for dates in date %}
                    <input type="radio" id="model_date" value="{{ dates.id }}" name="model_date" onclick="myFunJs( '{{ datas.date|date:'d' }}' );">{{ datas.data }} 
                    {% endfor %}
                    <input type="button" name="previous" class="previous action-button-previous" value="Previous"/>
                    <input type="button" name="next" class="next action-button" value="Next"/>
                </fieldset>
                <!--time-->
                <fieldset>
                    <table>
                        <tr>
                            <th>Time</th>
                            <th>seats</th>
                            <th>schedule</th>
                        </tr>
                        {% for times in time %}
                        <tr>
                            <td>{{ times.time}}</td>
                            <td> vagass
                                    {% with result=scheduling|my_filter_day:varJsToDjango %}
                                        {{ result|my_filter_time:times.time }}
                                    {% endwith  %}
                            </td>
                            <td><input type="radio" value="{{ times.id }}" name="model_time"></td>
                        </tr>
                        {% endfor %}
                    </table>
                    <input type="button" name="previous" class="previous action-button-previous" value="Previous"/>
                    <input type="submit" name="submit" class="submit action-button" value="Submit"/>
                </fieldset>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script type="text/javascript">
    function myFunJs(input) {
        var varInp = input;
        $.ajax({
            url: "{% url 'my_url' %}",
            type: "GET",
            data: {varJsToDjango: varInp},
            success: function(data){
                alert(data);
            }
        });
    };
</script>
</body>

{% endblock %} 

my view:

   def my_view(request):
   date = date.objects.all()
   time = time.objects.all()
   scheduling= scheduling.objects.all()
context = {
    'date ': date,
    'time ': time ,
    'scheduling': scheduling,
    'varJsToDjango': 6,   #start value to test, but I need change it to validate my table
}
if request.is_ajax():
    if request.method == 'GET':
        dados = request.GET.get('varJsToDjango')
        dados.save()

return render(request, 'my_url.html', context)

my filter:

from my_model.models import scheduling, time, date

register = template.Library()

@register.filter()
def my_filter_day(value, arg):
    result = (scheduling.objects.filter(Q(day__date__icontains=arg)))
    return result

@register.filter()
def my_filter_time(value, arg):
    result = (value.filter(Q(time__time__icontains=arg))).count()
    return result

if anyone could help me, it will be awesome. I'm stucked at this part few weeks. Any more information needed, let in the comment. I try be more specific.

0

There are 0 answers