简体   繁体   English

Django Web App Code可在本地开发机上运行,​​但在部署到服务器时无法运行

[英]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. 我有一个正在工作的Django Web项目,总体进展顺利。 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. 当我在本地计算机上运行它时,它按预期运行,当我将其部署到托管在Amazon EC2实例上的Web服务器时,它无法正确计算百分比。 I either get 100% or 0% no in between. 我要么没有得到100%或0%。

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. 该应用程序已通过Distelli部署到服务器上,该程序的所有其他方面均正常运行。 I can't quite understand how this works perfectly on my local machine and it does not on the web server. 我不太了解它如何在我的本地计算机上完美运行,而在Web服务器上却没有。

I'd check the Python version on the webserver. 我会在网络服务器上检查Python版本。 One way I could see this happening is if you are using Python 3 locally and Python 2 on the server. 我看到这种情况的一种方法是,如果您在本地使用Python 3,而在服务器上使用Python 2。

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. 以前,它会默默地覆盖小于1的整数除法的结果,使结果为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. 对于唯一的其他方式percentage_right评估为0是,如果minutes_right等于0,除非你已经创建的目的,这种情况下,在您的测试数据似乎不太可能。

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 . 您看到的100%结果可能是由于您的查询wrongs = Occurrence.objects.all()没有任何结果,或者由于wrongs = Occurrence.objects.all() wrong.TimeWrong所有结果都为零而wrong.TimeWrong

TL;DR Either a Python version mismatch or your data isn't what you're expecting on the webserver. TL; DR要么是Python版本不匹配,要么是您的数据与Web服务器上的期望值不符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用数据库连接将代码部署到Azure Web时,Flask应用程序无法呈现,但是在本地服务器上运行良好 - Flask app fails to render when deploying code to Azure Web with a database connection, but works fine from local server 使用部署到Heroku的Django应用程序时出现内部服务器错误。 该应用程序在本地工作 - I have an internal server error when using Django app deployed to Heroku. The app works locally django 密码重置在服务器上不起作用,但在本地计算机上起作用 - django password reset does not work on server but works on local machine django order_by 不在服务器上工作,但在本地机器上工作正常 - django order_by not working on server but works fine on local machine 托管在Apache上的Django应用程序失败,但在Dev服务器上工作正常 - Django application fails when hosted on Apache but works fine on Dev server 部署在 Heroku 上时更新 Django web 应用程序内容的最佳实践 - Best practice in updating Django web app content when deployed on Heroku 在 Gcloud 上部署时获取 Flask 开发服务器的服务器错误,但由于许可,作为本地服务器工作正常 - Obtain server error for Flask development server when deployed on Gcloud but works fine as local server due to permission Django:应用程序在服务器上部署时显示 400 错误 - Django: app displays 400 error when deployed on server Cron在本地主机上工作,但在部署Appengine时不工作 - Cron works on local host but not when deployed Appengine 提交表单后,Django 本地开发服务器挂起 chrome。 在火狐中工作 - Django local dev server hangs with chrome after form submission. Works in firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM