Undefined Movie Name and City Name in Seats HTML Template Despite Passing Through JavaScript and URL in Django Application

20 views Asked by At

this is my html code of shows.html

{% for cinema_hall in cinema_halls %}
    <div>
        <h2>{{ cinema_hall.name }}</h2>
        <div class="show-container" id="show-container-{{ cinema_hall.id }}">
            <!-- Loop through shows for each cinema hall -->
            {% for show in shows %}
                {% if show.cinemahall == cinema_hall %}
                    <div class="show-box" data-cinema_hall_id="{{ cinema_hall.id }}" data-show_id="{{ show.id }}">
                        Show: {{ show.timings }}
                        <br>
                         ₹ {{ show.price }} 
                    </div>
                {% endif %}
            {% endfor %}
        </div>
    </div>
{% endfor %}

and this is its js code


    function filterShowsByDate(date) {
        const movieName = '{{ movie_name }}'; // Replace with actual movie name
        const city = '{{ request.GET.city }}'; // Replace with actual city name

        $.ajax({
            url: `/shows/${movieName}/filter/${date}/?city=${city}`,
            type: 'GET',
            success: function(response) {
                // Clear existing show containers
                $('.show-container').empty();

                // Loop through the filtered shows and display them
                response.shows.forEach(function(show) {
    const showBox = `<div class="show-box" data-show-id="${show.id}" data-cinema-hall-id="${show.cinemahall_id}">Show: ${show.timings} <br> ₹ ${ show.price } </div>`;
    $('#show-container-' + show.cinemahall_id).append(showBox);
});


                $('.show-box').click(function(event) {
    event.preventDefault(); // Prevent default behavior
    
    const showId = $(this).data('show-id');
    const cinemaHallId = $(this).data('cinema-hall-id');
    const movieName = $(this).data('movie-name');
    const cityName = $(this).data('city-name');
    
    // Redirect to seats page with appropriate parameters
    window.location.href = `/seats/?show_id=${showId}&cinema_hall_id=${cinemaHallId}&movie_name=${encodeURIComponent(movieName)}&city_name=${encodeURIComponent(cityName)}`;
});
                 


            },
            error: function(xhr, errmsg, err) {
                console.log(errmsg);
            }
        });
    }

and this is views.py

def filter_shows_by_date(request, movie_name, selected_date):
    selected_city = request.GET.get('city')
    
    # Filter shows based on movie name, city, and selected date
    shows = Show.objects.filter(movie__name=movie_name, city__name=selected_city, date=selected_date)
    
    # Prepare data to be sent as JSON response
    shows_data = list(shows.values())

    response = {
        'movie_name': movie_name,
        'shows': shows_data,
        'selected_date': selected_date,
    }

    return JsonResponse(response)


def seats(request):
    show_id = request.GET.get('show_id')
    cinema_hall_id = request.GET.get('cinema_hall_id')
    movie_name = request.GET.get('movie_name')
    city_name = request.GET.get('city_name')

    # Retrieve show details
    show = Show.objects.get(id=show_id)

    # Retrieve cinema hall details
    cinema_hall = CinemaHall.objects.get(id=cinema_hall_id)

    context = {
        'show': show,
        'cinema_hall': cinema_hall,
        'movie_name': movie_name,  # Pass movie name
        'city_name': city_name  # Pass city name
    }

    return render(request, 'screen/seats.html', context)

and this is urls.py

  path('shows/<str:movie_name>/', views.shows, name='shows'),
  path('shows/<str:movie_name>/filter/<str:selected_date>/', views.filter_shows_by_date, name='filter_shows_by_date'),
 path('seats/',views.seats, name='seats' ) ,

now what want is that when user click on respective show div its get directed to seats.html , which is working good , but in seats.html i want, it to show movie, in front of movie , city in front od city and etc this is the seats.html code


 <p> 
        Movie name: {{ movie_name }} <br>
        City: {{ city_name }} <br>
        Cinema hall: {{ cinema_hall.name }} <br>
        Date: {{ show.date }} <br>
        <!-- Add other relevant show information here -->
    </p>

and i am not getting things here and in url also the movie name and city name are undefined, so please help me

0

There are 0 answers