访问列表或字典值以在视图函数中使用
accessing list or dictionary values for use in view function
假设我使用循环生成变量列表或字典:
a = {}
b = []
k = 1
count = TaskBox.objects.all().count()
output = ""
while k <= count:
ck = str(k)
newline = output.join("box"+ck+" = InboxEntry.objects.filter(box="+ck+")")
c.append(newline)
b['box'+ck] = "InboxEntry.objects.filter(box="+ck+")"
k += 1
产量:['box1 = InboxEntry.objects.filter(box=1)'、'box2 = InboxEntry.objects.filter(box=2)']
和
{'box2': 'InboxEntry.objects.filter(box=2)', 'box1': 'InboxEntry.objects.filter(box=1)'}
如何从任一结构中解压变量以便在 views.py 中使用?
我最终要寻找的是:
def display_prdInboxEntry(request, id):
if request.method == 'POST':
form = PrdInboxForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('taskmanager/display/'+ id +'/')
else:
form = PrdInboxForm(request.POST)
return HttpResponseRedirect('taskmanager/display/'+ id +'/')
else:
form = PrdInboxForm()
user = request.user
u = UserProfile.objects.get(pk=id)
boxrecords = TaskBox.objects.all()
assignedrecords = InboxEntry.objects.filter(assigned_by=u)
'''loop code'''
a = {}
b = []
k = 1
count = TaskBox.objects.all().count()
output = ""
while k <= count:
ck = str(k)
newline = output.join("box"+ck+" = InboxEntry.objects.filter(box="+ck+")")
c.append(newline)
b['box'+ck] = "InboxEntry.objects.filter(box="+ck+")"
k += 1
'''need to pull these values from the product of the loop'''
box1 = InboxEntry.objects.filter(box=1)
box2 = InboxEntry.objects.filter(box=2)
records = InboxEntry.objects.filter(assigned_to=u).order_by('status')
return render_to_response('taskmanager/taskmanager_view.html', {'form': form, 'assignedrecords': assignedrecords, 'records': records, 'boxrecords': boxrecords, 'box1' : box1, 'box2' : box2, 'user': user}, context_instance=RequestContext(request))
好的,所以我不需要 "extract" 字典中的变量来使用它们。我只是在我的视图代码中放了一个循环来生成变量字典,然后将该字典与我的 http 响应字典连接起来:
def display_prdInboxEntry(request, id):
if request.method == 'POST':
'''form code'''
else:
form = PrdInboxForm()
user = request.user
u = UserProfile.objects.get(pk=id)
assignedrecords = InboxEntry.objects.filter(assigned_by=u)
records = InboxEntry.objects.filter(assigned_to=u).order_by('status')
boxrecords = TaskBox.objects.all()
boxes = TaskBox.objects.all().count()
''' variable generator'''
boxdict = {}
for x in range(1, boxes + 1):
boxdict['box{0}s'.format(x)] = InboxEntry.objects.filter(box='{0}'.format(x))
returndict = {'form': form, 'assignedrecords': assignedrecords, 'records': records, 'boxrecords': boxrecords, 'user': user}
''' return dictionary concatenation'''
z = returndict.copy()
z.update(boxdict)
return render_to_response('taskmanager/taskmanager_view.html', z, context_instance=RequestContext(request))
不知道它有多传统,有效地编写模板代码将是一个挑战 - 但它可以工作,并且允许用户在管理中即时创建任务框,而无需进行调整查看代码 - 这就是我想要的。
假设我使用循环生成变量列表或字典:
a = {}
b = []
k = 1
count = TaskBox.objects.all().count()
output = ""
while k <= count:
ck = str(k)
newline = output.join("box"+ck+" = InboxEntry.objects.filter(box="+ck+")")
c.append(newline)
b['box'+ck] = "InboxEntry.objects.filter(box="+ck+")"
k += 1
产量:['box1 = InboxEntry.objects.filter(box=1)'、'box2 = InboxEntry.objects.filter(box=2)']
和 {'box2': 'InboxEntry.objects.filter(box=2)', 'box1': 'InboxEntry.objects.filter(box=1)'}
如何从任一结构中解压变量以便在 views.py 中使用?
我最终要寻找的是:
def display_prdInboxEntry(request, id):
if request.method == 'POST':
form = PrdInboxForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('taskmanager/display/'+ id +'/')
else:
form = PrdInboxForm(request.POST)
return HttpResponseRedirect('taskmanager/display/'+ id +'/')
else:
form = PrdInboxForm()
user = request.user
u = UserProfile.objects.get(pk=id)
boxrecords = TaskBox.objects.all()
assignedrecords = InboxEntry.objects.filter(assigned_by=u)
'''loop code'''
a = {}
b = []
k = 1
count = TaskBox.objects.all().count()
output = ""
while k <= count:
ck = str(k)
newline = output.join("box"+ck+" = InboxEntry.objects.filter(box="+ck+")")
c.append(newline)
b['box'+ck] = "InboxEntry.objects.filter(box="+ck+")"
k += 1
'''need to pull these values from the product of the loop'''
box1 = InboxEntry.objects.filter(box=1)
box2 = InboxEntry.objects.filter(box=2)
records = InboxEntry.objects.filter(assigned_to=u).order_by('status')
return render_to_response('taskmanager/taskmanager_view.html', {'form': form, 'assignedrecords': assignedrecords, 'records': records, 'boxrecords': boxrecords, 'box1' : box1, 'box2' : box2, 'user': user}, context_instance=RequestContext(request))
好的,所以我不需要 "extract" 字典中的变量来使用它们。我只是在我的视图代码中放了一个循环来生成变量字典,然后将该字典与我的 http 响应字典连接起来:
def display_prdInboxEntry(request, id):
if request.method == 'POST':
'''form code'''
else:
form = PrdInboxForm()
user = request.user
u = UserProfile.objects.get(pk=id)
assignedrecords = InboxEntry.objects.filter(assigned_by=u)
records = InboxEntry.objects.filter(assigned_to=u).order_by('status')
boxrecords = TaskBox.objects.all()
boxes = TaskBox.objects.all().count()
''' variable generator'''
boxdict = {}
for x in range(1, boxes + 1):
boxdict['box{0}s'.format(x)] = InboxEntry.objects.filter(box='{0}'.format(x))
returndict = {'form': form, 'assignedrecords': assignedrecords, 'records': records, 'boxrecords': boxrecords, 'user': user}
''' return dictionary concatenation'''
z = returndict.copy()
z.update(boxdict)
return render_to_response('taskmanager/taskmanager_view.html', z, context_instance=RequestContext(request))
不知道它有多传统,有效地编写模板代码将是一个挑战 - 但它可以工作,并且允许用户在管理中即时创建任务框,而无需进行调整查看代码 - 这就是我想要的。