[英]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.