我正在處理視圖。我正在嘗試創建一個基於記錄模型顯示錶單的html文件。在html文件中,我添加了將顯示組中所有成員的代碼。每個成員旁邊都有一個複選框。在view.py文件中,我想通過每個成員,如果複選框被選中,我想創建一個新的transacition對象並將其保存到數據庫,如果它實際上被檢查。我得到一個錯誤,它看起來像這樣...檢查Django中選中的複選框的項目
錯誤:
KeyError at /23/addRecord/
'omar'
Request Method: POST
Request URL: http://127.0.0.1:8000/23/addRecord/
Django Version: 1.8.6
Exception Type: KeyError
Exception Value:
'omar'
Exception Location: C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in addRecord, line 237
Python Executable: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\OmarJandali\\Desktop\\opentab\\opentab',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
Server time: Mon, 3 Jul 2017 07:02:20 +0000
Traceback Switch to copy-and-paste view
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in addRecord
username = cd[member.user.username] ...
▶ Local vars
Request information
GET
No GET data
POST
Variable Value
csrfmiddlewaretoken
'vIg5KKWVNpoc2ijkwmBB9I28aDt7QOSA'
split
'2'
omar
'checked'
hani
'checked'
submit
'submit'
這裏是我現在所擁有的文件。
view.py
def addRecord(request, groupId):
# for the records, the user and group need to be provided to know what group
# this record is attached to and who created the record.
user = User.objects.get(username='omar')
group = Group.objects.get(id=groupId)
members = Member.objects.filter(group=groupId)
# the followin process is similar to the form validation for the group view.
if request.method == 'POST':
message = 'process'
form = AddRecordForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
split = cd['split']
new_record = Record.objects.create(
split = split,
status = 1,
group = group,
user = user,
)
for member in members:
if member.user.username in request.GET:
new_transaction = Transaction.objects.create(
amount = 0.00,
description = 'description',
group = group,
user = member,
record = new_record,
)
return redirect('accounts')
else:
# the only tyhing that needed to be passed was the group to display the naem
# and the form that is going to be used and filled out by the user and submitted
form = AddRecordForm()
message = 'no processing'
params = {
'group':group,
'members':members,
'form':form,
'message':message,
}
return render(request, 'tabs/add_record.html', params)
HTML文件:
{% extends "base.html" %}
{% block content %}
<h2>add record to {{ group.name }}</h2>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{{ form.as_p }}
{% for member in members %}
{{ member.user.username }}
<input name="{{member.user.username}}" value="checked" type="checkbox"><br>
{% endfor %}
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
models.py文件
class Record(models.Model):
split = models.SmallIntegerField(default=1) #user
count = models.IntegerField(default=1)
status = models.SmallIntegerField(choices=RECORD_STATUS_CHOICES, default=1) #server
group = models.ForeignKey(Group, default=1, on_delete=models.CASCADE) #server
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE) #server
created = models.DateTimeField(auto_now_add=True) #server
class Transaction(models.Model):
amount = models.DecimalField(decimal_places=2, max_digits=9,default=0.00)
description = models.CharField(max_length=250)
group = models.ForeignKey(Group, default=1, on_delete=models.CASCADE)
user = models.ForeignKey(User, default=1, on_delete=models.CASCADE)
record = models.ForeignKey(Record, default=1, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
我已經嘗試做一些不同的事情,包括改變形式輸入名稱和值。我嘗試了幾種不同的方法來改變cd []的內容,以及如何檢查它是否存在發生錯誤的地方。任何人都可以幫助我解決這個棘手的問題。
問題與get方法。沒有名爲omar的用戶名,請檢查數據庫以獲取實際名稱 – Exprator
檢查'print(cd)' –
的值在我的本地數據庫中有一個名稱爲omar的用戶。我正在使用我正在做的所有不同的測試。當顯示html文件時,它顯示用戶名omar旁邊的複選框。所以這就是爲什麼我想知道這個問題是什麼。 –