Django przekazywanie zmiennej z innego widoku jak to zrobic

0

Witam mam maly problem mam Widok :

 def showMondayTable(request):
    subjects = Subject.objects.all().filter(day=1)
    return render(request, 'signs/monday.html', {'subjects':subjects,'locals': locals()})

i Widok:

 def signMe(request, subject_id):

    user = request.user
    subject = get_object_or_404(Subject, id=subject_id)
    sign = Sign()
    user_is_participant = Sign.objects.all().filter(subject=subject_id, user=user).exists()

    try:
        sign.user = user
        sign.subject = subject
    finally:
        if subject.actual_space < subject.space:
            if user_is_participant == False:
                subject.actual_space = subject.actual_space + 1
            else:
                pass
            subject.save()
            sign.save()
        else:
            pass

    return render(request, 'signs/sign_success.html', {'user_is_participant': user_is_participant, 'locals': locals()})

Do tego template'a

 {% extends 'base.html' %}

{% block scripts %}
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
{% endblock %}

{% block pageContent %}
    <table class="table table-hover">
    <thead>
    <tr>
        <th>Subject</th>
        <th>Begin</th>
        <th>End</th>
        <th>Space</th>
        <th>Sign</th>
    </tr>
    </thead>

{% for subject in subjects %}
    <tbody>
        <td>{{ subject.name }}</td>
        <td>{{ subject.begin_at }}</td>
        <td>{{ subject.end_at }}</td>
        <td> {{ subject.actual_space }}/{{ subject.space }}</td>
        <td><button class="btn-success subjectButton" day="monday" {% if user_is_participant > 0 %} disabled {% endif %} subjectId="{{ subject.id }}"> Zapisz </button><br></td>
        <pre>{{ locals }}</pre>
    </tbody>
{% endfor %}

    </table>
    <script>

        $(document).ready(function () {
            $('.subjectButton').click(function(){
                var day = $(this).attr('day');
                var subjectId = $(this).attr('subjectId');
                var button = $(this);
                button.prop('disabled', true);
                button.text("Zapisano!");
                console.log('dzien: '+day+', id: '+subjectId)

                $.ajax({
                    url: 'http://localhost:8000/sign/'+day+'/signme/'+subjectId
                }).done(function(data){
                    console.log('subject send');
                    console.log('recived data: '+ JSON.stringify(data));
                });

            })
        })


</script>
{% endblock %}



Chcialem z widoku SignMe przekazac zmienna do monday.html ale nie moge tego zrobic gdyz template pobral zmienne z showMondayTable jak to obejsc? musze zablokowac przycisk jesli user jest zapisany ma zostac napis "Zapisano" i przycisk ma byc nieaktywny a terraz bo odswiezeniu strony przycisk jest nadal aktywny

1

Musisz w glównym widoku (tym, który jest wykorzystywany w momencie wejścia na stronę) stworzyć np. słownik z boolami subject_participation = {'subject': true, 'another_subject': false ... } a w templatce

<td><button class="btn-success subjectButton" day="monday" {% if subject_participation[subject.id] %} disabled {% endif %} subjectId="{{ subject.id }}"> Zapisz </button><br></td>
0

@stivens: jakis inny pomysl? nie mam pojecia jak sie za to zabrac

1

A w jaki sposób próbowałeś zaimplementować tą solucje?

W głównym widoku bierzesz baze danych, dla każdego kursu sprawdzasz czy user z requesta jest zapisany i przekazujesz taki słownik do templatki

1 użytkowników online, w tym zalogowanych: 0, gości: 1