我正在构建一个表单,供用户更新对象中的字段。
这是一个页面,其中包含用于更新模型的不同实例的表单列表。大多数表单都可以正常工作,但当我尝试在页面中提交第一个表单时,我收到一个错误:输入对象' word'没有属性'更新'。我没有一个名为“' word'并且只有在我尝试在页面上提交第一个表单时才会出现此错误。有任何想法吗?提前致谢!
在模板中:
{% for corr in correction_list %}
<tr><td>{{corr.correction_author}}</td><td>{{corr.time | date:"m/d/Y"}}</td>
<td>{{corr.corrected_word.kwicl}}</td><td>{{corr.corrected_word.keyword}}</td>
<td>{{corr.corrected_word.kwicr}}</td><td>{{corr.corrected_word.choice1}}</td>
<td>{{corr.corrected_word.choice2}}</td><td>{{corr.corrected_word.choice3}}</td>
<td>{{corr.corrected_word.correct_choice}}</td><td>{{corr.corrected_word.manually_entered_word}}</td>
<td>{{corr.correction_made}}</td><td>{{corr.correction_word}}</td>
<td><form action="../approval/" method="POST">
{% csrf_token %}
Decision:
<select name="approved">
<option value=1>Approve</option>
<option value=2>Reject</option>
<option value=0>Hold</option>
</select><br>
<input type="text" name="corrected_word" value={{corr.corrected_word.file_position}} hidden="True"/>
<input type="number" name="correction_made" value={{corr.correction_made}} hidden="True"/>
{% if corr.correction_word %}
<input type="text" name="correction_word" value={{corr.correction_word}} hidden="True"/>
{% else%}
<input type="text" name="correction_word" value="" hidden="True"/>
{% endif %}
<input type="submit"/></form></td></tr>
{% endfor %}
在views.py中:
def submit_approval(request):
if hasattr(request, 'user') and request.user.is_authenticated():
corrected_word = request.POST['corrected_word']
updateword = item.objects.filter(file_position=corrected_word)[0]
correction_made = request.POST['correction_made']
correction_word = request.POST['correction_word']
approval = request.POST['approved']
corr = correction.objects.filter(corrected_word=updateword).filter(correction_made=correction_made).filter(correction_word=correction_word)
if request.method == 'POST':
for entry in corr:
entry.approved = approval
entry.save()
return HttpResponse('thanks')
return render(request, "revision.html")
在urls.py中:
url(r'^approval/$', 'english.views.submit_approval'),
回溯:
Request Method: POST
Request URL: http://localhost:8000/revision/
Django Version: 1.8.3
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'english')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/Users/vl/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/vl/english/english/views.py" in revision
151. word.update(approved=formdata)
Exception Type: AttributeError at /revision/
Exception Value: type object 'word' has no attribute 'update'
答案 0 :(得分:0)
问题在于您的action
代码形式。在django中对任何url进行硬编码通常是一种不好的做法。如果您的网址发生变化,您必须随处更改。查看文档:{{3}}。
根据该文件,最佳做法是:
url(r'^approval/$', 'english.views.submit_approval', name="submit-approval"),
<form action="{% url 'submit-approval' %}" method="POST"></form>