1

i cannot get objects from my database. i'm getting nothing. in terminal i'm not getting error and i'm entering on website without problem. i want get information from .

 DT = Destination_Tour.objects.all()

my Views.py

def index(request):
min_date = f"{datetime.now().date().year}-{datetime.now().date().month}-{datetime.now().date().day}"
max_date = f"{datetime.now().date().year if (datetime.now().date().month+3)<=12 else datetime.now().date().year+1}-{(datetime.now().date().month + 3) if (datetime.now().date().month+3)<=12 else (datetime.now().date().month+3-12)}-{datetime.now().date().day}"
citynames = Place.objects.all()
slider = Slider.objects.all()
DT = Destination_Tour.objects.all()
if request.method == 'POST':
    origin = request.POST.get('Origin')
    destination = request.POST.get('Destination')
    depart_date = request.POST.get('DepartDate')
    seat = request.POST.get('SeatClass')
    trip_type = request.POST.get('TripType')
    citynames = Place.objects.all()
    if(trip_type == '1'):
        return render(request, 'base.html', {
        'origin': origin,
        'destination': destination,
        'depart_date': depart_date,
        'seat': seat.lower(),
        'trip_type': trip_type,
        'name':citynames,
    })
else:
    return render(request, 'base.html', {
        'min_date': min_date,
        'max_date': max_date
    })
context = {
    'name':citynames,
    'sd': slider,
    'DT' : DT,
    }

return render(request,'base.html', context = context)
1
  • Did you try to get all data using django shell? type python manage.py shell and then import your model and type queryset Destination_Tour.objects.all(). Did it work? Commented Aug 5, 2022 at 13:20

1 Answer 1

1

this is because you are not passing your variable in the proper place,

look closely, your index view got executed before you pass your DT variable via context

with POST and GET methods you can only return function two times, but you used it three times, and third time will never be executed

it should be like this:

def index(request):
    min_date = f"{datetime.now().date().year}-{datetime.now().date().month}-{datetime.now().date().day}"
    max_date = f"{datetime.now().date().year if (datetime.now().date().month+3)<=12 else datetime.now().date().year+1}-{(datetime.now().date().month + 3) if (datetime.now().date().month+3)<=12 else (datetime.now().date().month+3-12)}-{datetime.now().date().day}"
    citynames = Place.objects.all()
    slider = Slider.objects.all()
    DT = Destination_Tour.objects.all()

    #also good practice for debuging is to print your variables in terminal
    print(DT)

    if request.method == 'POST':
        origin = request.POST.get('Origin')
        destination = request.POST.get('Destination')
        depart_date = request.POST.get('DepartDate')
        seat = request.POST.get('SeatClass')
        trip_type = request.POST.get('TripType')
        citynames = Place.objects.all()
        if(trip_type == '1'):
            return render(request, 'base.html', {
            'origin': origin,
            'destination': destination,
            'depart_date': depart_date,
            'seat': seat.lower(),
            'trip_type': trip_type,
            'name':citynames,
            'DT' : DT,
        })
    else:
        return render(request, 'base.html', {
            'min_date': min_date,
            'max_date': max_date
            'DT' : DT,
        })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.