ATENÇÃO!

Caso tenha dificuldade em copiar os códigos pelo PDF acesse diretamente pelo NOTION:

https://grizzly-amaranthus-f6a.notion.site/PYSTACK-WEEK-2-0-Aula-3-cbba30c1e0084f04a3c0cb8c71942a34

Crie o modal para agendar visita

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title titulo_filtrar" id="exampleModalLabel">Agendar visita</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="" method="POST">{% csrf_token %}
                        <label>Escolha um dia</label>
                        <select name="dia" class="form-control">
                            {% for dia in imovel.dias_visita.all %}
                                <option value="{{dia}}">{{dia}}</option>
                            {% endfor %}
                        </select>
                        <br>

                        <label>Escolha um horário</label>
                        <select name="horario" class="form-control">
                            {% for horario in imovel.horarios.all %}
                                <option value="{{horario}}">{{horario}}</option>
                            {% endfor %}
                        </select>
                        <input name="id_imovel" type="hidden" value="{{id}}">
                        <br>
                        <input  class="btn_agendar" type="submit" value="AGENDAR">
                    </form> 
                </div>
                
            </div>
        </div>
    </div>

Crie a URL agendar visitas

path('agendar_visitas', views.agendar_visitas, name="agendar_visitas"),

Crie a view agendar_visitas

def agendar_visitas(request):
    usuario = request.user
    dia = request.POST.get('dia')
    horario = request.POST.get('horario')
    id_imovel = request.POST.get('id_imovel')

    visita = Visitas(
        imovel_id=id_imovel,
        usuario=usuario,
        dia=dia,
        horario=horario
    )
    visita.save()

    return redirect('/agendamentos')

Defina a action no formulário

{% url 'agendar_visitas' %}

Vamos para o agendamento

path('agendamentos', views.agendamentos, name="agendamentos"),

Crie a views de agendamentos

def agendamentos(request):
    visitas = Visitas.objects.filter(usuario=request.user)
    return render(request, "agendamentos.html", {'visitas': visitas})

Agora precisamos criar o agendamentos.html

{% extends 'base.html' %}
{% load static %}

{% block 'head' %}

<link rel="stylesheet" href="{% static 'plataforma/css/home.css' %}">

{% endblock  %}

{% block 'body' %}

    <div class="container">
        
        <br>
        <table class="table table-striped">
        <thead>
            <tr>
                <th scope="col">img</th>
                <th scope="col">Endereco</th>
                <th scope="col">Dia/Hora</th>
                <th scope="col">Status</th>
                <th scope="col">Cancelar</th>
            </tr>
        </thead>
        <tbody>
            {% for visita in visitas %}
                <tr>
                    <th><img width="80" src="{{visita.imovel.imagens.all.0}}"></th>
                    <td>{{visita.imovel.rua}}, {{visita.imovel.cidade}}</td>
                    <td>{{visita.dia}}/{{visita.horario}}</td>
                    <td>
                        {% if visita.status == "A" %}
                            <span class="badge badge-info">Agendado</span>
                        {% endif %}

                        {% if visita.status == "F" %}
                            <span class="badge badge-success">Finalizado</span>
                        {% endif %}

                        {% if visita.status == "C" %}
                            <span class="badge badge-danger">Cancelado</span>
                        {% endif %}
                    </td>
                    <td>

                        {% if visita.status == "A" %}
                            <a href="" class="btn btn-danger">CANCELAR</a>
                        {% else %}
                            <a href="" class="btn btn-danger disabled">CANCELAR</a>
                        
                        {% endif %}

                        
                    
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    </div>
    

{% endblock  %}

Para cancelar uma visita vamos criar a URL:

path('cancelar_agendamento/<str:id>', views.cancelar_agendamento, name="cancelar_agendamento")