简体   繁体   中英

Django Web App Code works on local dev machine but not when deployed to server

I have a Django web project that i am working on which overall has gone pretty smoothly. I have a view below which grabs objects from the database and displays them via a template to the user.

def index(request):
    all_occurrences = Occurrence.objects.all().order_by("-pk")
    calculate_percentage_right()

    context = {
        'all_occurrences': all_occurrences,
        'percentage_right': calculate_percentage_right()
    }
    query = request.GET.get("q")
    if query:
        all_occurrences = all_occurrences.filter(
            Q(Subject__icontains=query) |
            Q(Details__icontains=query))
        return render(request, 'wrong/wrong_view.html', {
            'all_occurrences': all_occurrences,
        })
    else:
        return render(request, 'wrong/wrong_view.html', context)

The issue that is occurring which to me makes no sense to me is that I am using a function to calculate a percentage right which is based on an object attribute called "timewrong". When I run this on my local machine it performs as expected, when I deploy this to my web server which is hosted on an Amazon EC2 Instance it does not calculate the percentage correctly. I either get 100% or 0% no in between.

I have verified that on the webserver it is seeing all of the variables I have created correctly. The problems appears to occur when the division happens to get me to the percentage.

def calculate_percentage_right():
    wrongs = Occurrence.objects.all()
    total_minutes_wrong = 0

    for wrong in wrongs:
        total_minutes_wrong += wrong.TimeWrong

    minutes_in_year = 525600
    minutes_right = minutes_in_year - total_minutes_wrong
    percentage_right = (minutes_right / minutes_in_year) * 100

    return percentage_right

The app is being deployed via Distelli to the server, all other aspects of the program is working as it should. I can't quite understand how this works perfectly on my local machine and it does not on the web server.

I'd check the Python version on the webserver. One way I could see this happening is if you are using Python 3 locally and Python 2 on the server.

The behaviour of the division operator changed as described here . Previously it would silently floor results of integer divisions that were less than 1, giving you the result 0.

The only other way for percentage_right to evaluate to 0 would be if minutes_right was equal to 0. That seems unlikely unless you've created that case on purpose in your test data.

The 100% results you saw could be explained either by there being no results for your query wrongs = Occurrence.objects.all() or by all of the results having a value of zero for wrong.TimeWrong .

TL;DR Either a Python version mismatch or your data isn't what you're expecting on the webserver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM