【发布时间】:2017-11-28 04:51:56
【问题描述】:
我正在 Django 中创建一个应用程序,它允许我的用户根据已经存储在数据库中的信息从我的站点订购商品。
并非我的所有用户都应该能够订购某些商品,为此我编写了一个带有比较语句的管道并尝试,除了块。
一小段可重现的代码如下所示:
vendor.py
def guest_constraint(request)
# Ensure user in request is a house-guest by checking if it has an active token.
try:
guest = GuestProfile.objects.get(user=request.user.id)
except ObjectDoesNotExist:
return render(request, 'extGuest/appGuestError/not_hotel_login.html')
# Check for Hotel Room Information linked to Guest Token
try:
room_information = RoomInformation.objects.get(guest_token=guest.token)
except ObjectDoesNotExist:
return render(request, 'extGuest/appGuestError/constraint_error.html')
views.py
from .vendor import guest_constraint
@login_required
def index(request):
guest_contraint(request) # Filter out users with no access to this process.
user = request.user # Grab user defined in request.
name = user.get_short_name() # Grab first name of user.
return render(request, 'extGuest/appGuestFlow/choose_order_type.html')
挑战:我可以成功地将这个小脚本导入到我的视图中,并且我可以看到它的内容正在运行 except for the return render(request , 模板) 部分。
为了更好地解释我自己,try/except 块成功捕获了异常,但是它不返回块中指定的模板,而是返回视图并呈现我在视图中的模板。
我尝试了什么?如果我将 guest_constraint (vendor.py) 的代码放在 index (views.py) 中,我认为没有问题并且可以按预期工作。然而,这并不能很好地扩展,因为我希望在 views.py 中为许多不同的函数调用 guest_contraint
我对编程和 Django 还很陌生,我还有很多东西要学。如果你能告诉我你认为我做错了什么,或者关于 Django(甚至是基本的 Python)的什么主题可以帮助我解决这个问题,我将不胜感激。谢谢!
编辑:我忘了说,我使用的是 Django 1.11.6。
编辑 2: 哎呀,我忘了包括我如何在我的索引视图中使用该功能。我的错,对不起。
解决方案:
感谢 @cwallenpoole 以及我对他的回复所做的一些更改,我能够编辑我的代码来做我想做的事情,现在看起来像这样:
供应商.py |已更新
def guest_constraint(function):
def _inner_guest_constraint(request)
# This part should be familiar
try:
guest = GuestProfile.objects.get(user=request.user.id)
except ObjectDoesNotExist:
return render(request, 'extGuest/appGuestError/not_hotel_login.html')
try:
room_information = RoomInformation.objects.get(guest_token=guest.token)
except ObjectDoesNotExist:
return render(request, 'extGuest/appGuestError/constraint_error.html')
# Once all checks are passed return flow back to function.
return function(request)
# return the wrapping
return _inner_guest_constraint
views.py |已更新
from .vendor import guest_constraint
@login_required
@guest_constraint
def index(request):
user = request.user # Grab user defined in request.
name = user.get_short_name() # Grab first name of user.
return render(request, 'extGuest/appGuestFlow/choose_order_type.html')
【问题讨论】:
-
我没有看到您的
index视图中使用的guest_constraint方法。 -
抱歉,我不小心遗漏了如何在 views.py 函数中使用该函数。现在已经更正了。
标签: python django django-templates django-views django-middleware