比方說,我有一個做數據庫的東西,需要已通過POST發送用戶身份驗證的形式,有可能裏面要求有人邪改變用戶以利用系統?Django/Auth:可以request.user被利用並指向其他用戶嗎?
以下示例在數據庫中創建一個項目,但需要登錄的用戶。有人可以發送其他用戶的數據request.user?
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from items_core.models import Item
from items.forms import CreateItemForm
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
@login_required
def create(request):
errors = None
if request.method == 'POST':
form = CreateItemForm(request.POST)
if form.is_valid():
try:
Item.objects.get(
name = form.cleaned_data['name'],
user = request.user
)
errors = 'Item already exist. Please provide other name.'
except Item.DoesNotExist:
Item.objects.create(
name = form.cleaned_data['name'],
user = request.user
)
return redirect('items:list')
form = CreateItemForm()
else:
form = CreateItemForm()
template = {
'form':form,
'items':Item.objects.filter(user=request.user),
'request':request,
'errors':errors
}
return render(request, 'items/item_create.html', template)
謝謝!
是不是也在響應對象中傳遞了用戶對象,以便可以通過模板訪問它?由於安全問題,我有沒有辦法通過用戶對象? – user1050619 2015-12-22 03:17:39
@ user1050619在一箇中間件中,你可以這樣做:'del response ['user']'。還要確保你刪除了相應的上下文處理器。更多信息,請訪問:https://docs.djangoproject.com/en/1.9/ref/request-response/ – karthikr 2015-12-23 01:46:13