本文介绍了对象没有属性'get'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试编辑对象时出现错误:
我符合form.as_p:
{%extendslayout.html%}
{%block内容%}
< form action ={{form_url}}method =post> {%csrf_token%}
{{form.as_p}}
<输入类型=submitvalue =提交/>
< / form>
{%endblock%}
控制器代码:
def edit(request,id):
如果request.method =='POST':#如果表单已经提交...
form = IntentionForm(request.POST)#绑定到POST数据的表单
如果form.is_valid():#所有验证规则通过
#处理form.cleaned_data中的数据
# ...
intent = form.save()
return HttpResponseRedirect(reverse_lazy('intent-show',args = [intent.id]))#POST后重定向
else:
intent = Intention.objects.get(pk = id)
form = IntentionForm(intent)#未绑定的形式
返回render_to_response('intentions / templates / form.html',
{'form':form,'form_url':reverse_lazy('intent-edit',args = [intent.id])},
context_instance = RequestContext(req uest)
)
有人可以给我任何建议吗?
引用:
环境:
请求方法:GET
请求URL:http://127.0.0.1:8000/intentions/3/edit
Django版本:1.4
Python版本:2.7.3
已安装的应用程序:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites ',
'django.contrib.messages',
'django.contrib.staticfiles',
'intentions',
'django.contrib.admin')
安装的中间件:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Te mplate错误:
在模板/home/marek/devel/django/prayer/intentions/templates/form.html中,错误在第4行
'Intention'对象没有属性'get'
1:{%extendslayout.html%}
2:{%block content%}
3:< form action ={{form_url}}method =post> {%csrf_token%}
4:{{form.as_p}}
5:< input type =submitvalue =Submit/>
6:< / form>
7:{%endblock%}
8:
追溯:
文件/ usr / local / lib / python2.7 / dist-packages / django / core / handlers / base.pyin get_response
111. response = callback(request,* callback_args,** callback_kwargs)
文件/home/marek/devel/django/prayer/intentions/views.py在编辑
55. context_instance = RequestContext(请求)
文件/usr/local/lib/python2.7/dist return_to_response
20.返回HttpResponse(loader.render_to_string(* args,** kwargs),** httpresponse_kwargs)
文件/ usr / local / lib中的-packages / django / shortcuts / __ init__.py /python2.7/dist-packages/django/template/loader.pyin render_to_string
176. return t.render(context_instance)
文件/usr/local/lib/python2.7/dist render
140.中的-packages / django / template / base.py。return self._render(context)
文件/usr/local/lib/python2.7/dist-packages/django/template / BA se.py在_render
134. return self.nodelist.render(context)
文件/usr/local/lib/python2.7/dist-packages/django/template/base.py在render
823. bit = self.render_node(node,context)
文件/usr/local/lib/python2.7/dist-packages/django/template/debug.py在render_node
74. return node.render(context)
文件/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py在render
123. return compile_parent._render(context)
文件/usr/local/lib/python2.7/dist-packages/django/template/base.py在_render
134. return self.nodelist.render(上下文)
文件/usr/local/lib/python2.7/dist-packages/django/template/base.py在render
823. bit = self.render_node(node,context)
文件/usr/local/lib/python2.7/dist-packages/django/template/debug.py在render_node
中74.返回node.render(上下文)
文件/ USR / local / lib目录/ python2.7 / DIST-PAC kages / django / template / loader_tags.pyin render
62. result = block.nodelist.render(context)
文件/usr/local/lib/python2.7/dist-packages/django /template/base.pyin render
823. bit = self.render_node(node,context)
文件/usr/local/lib/python2.7/dist-packages/django/template/ render_node
74. return node.render(context)
文件/usr/local/lib/python2.7/dist-packages/django/template/debug.py在渲染中
84. output = self.filter_expression.resolve(context)
解析$ b $中的文件/usr/local/lib/python2.7/dist-packages/django/template/base.py b 571. obj = self.var.resolve(context)
解析
721中的文件/usr/local/lib/python2.7/dist-packages/django/template/base.py。 value = self._resolve_lookup(context)
文件/usr/local/lib/python2.7/dist-packages/django/template/base.py在_resolve_lookup
772. current = current()
文件/usr/local/lib/python2.7/dist-packages/django/forms/forms.pyin as_p
238. errors_on_separate_row = True)
文件 _html_output中的/usr/local/lib/python2.7/dist-packages/django/forms/forms.py
143. top_errors = self.non_field_errors()#应显示在所有字段上方的错误。
文件/usr/local/lib/python2.7/dist-packages/django/forms/forms.pyin non_field_errors
246. return self.errors.get(NON_FIELD_ERRORS,self.error_class( ))
文件/usr/local/lib/python2.7/dist-packages/django/forms/forms.py在_get_errors
115. self.full_clean()
文件 /usr/local/lib/python2.7/dist-packages/django/forms/forms.pyin full_clean
270. self._clean_fields()
文件/ usr / local / lib / python2 _clean_fields
281.。value = field.widget.value_from_datadict(self.data,self.files,self.add_prefix(name))
file_from_datadict中的文件/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py
205. return data.get(name,None)
异常类型:/ intentions / 3 / edit
中的AttributeError异常值:'Intention'对象没有属性'get'
解决方案
您的问题是h ere:
intent = Intention.objects.get(pk = id)
form = IntentionForm(intent)#An未绑定的形式
表单的第一个参数是数据,但您传递的是实例。要适当地传递您应该使用的实例:
intent = Intention.objects.get(pk = id)
form = IntentionForm(instance = intent)#未绑定的形式
I can't find any solution to my issue with similar kind of error.
The error is when I try to edit object:
I have it in line with form.as_p:
{% extends "layout.html" %}
{% block content %}
<form action="{{ form_url }}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
controller code:
def edit(request, id):
if request.method == 'POST': # If the form has been submitted...
form = IntentionForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
intention = form.save()
return HttpResponseRedirect(reverse_lazy('intention-show', args=[intention.id])) # Redirect after POST
else:
intention = Intention.objects.get(pk=id)
form = IntentionForm(intention) # An unbound form
return render_to_response('intentions/templates/form.html',
{'form': form, 'form_url': reverse_lazy('intention-edit', args=[intention.id])},
context_instance=RequestContext(request)
)
Could someone give me any advice?
Trackback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/intentions/3/edit
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'intentions',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template error:
In template /home/marek/devel/django/prayer/intentions/templates/form.html, error at line 4
'Intention' object has no attribute 'get'
1 : {% extends "layout.html" %}
2 : {% block content %}
3 : <form action="{{ form_url }}" method="post">{% csrf_token %}
4 : {{ form.as_p }}
5 : <input type="submit" value="Submit" />
6 : </form>
7 : {% endblock %}
8 :
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/marek/devel/django/prayer/intentions/views.py" in edit
55. context_instance=RequestContext(request)
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
176. return t.render(context_instance)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
140. return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render
84. output = self.filter_expression.resolve(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in resolve
571. obj = self.var.resolve(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in resolve
721. value = self._resolve_lookup(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _resolve_lookup
772. current = current()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in as_p
238. errors_on_separate_row = True)
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _html_output
143. top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in non_field_errors
246. return self.errors.get(NON_FIELD_ERRORS, self.error_class())
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _get_errors
115. self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean
270. self._clean_fields()
File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _clean_fields
281. value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py" in value_from_datadict
205. return data.get(name, None)
Exception Type: AttributeError at /intentions/3/edit
Exception Value: 'Intention' object has no attribute 'get'
解决方案
Your problem is here:
intention = Intention.objects.get(pk=id)
form = IntentionForm(intention) # An unbound form
The first argument to a form is the data but you are passing the instance. To properly pass the instance you should use:
intention = Intention.objects.get(pk=id)
form = IntentionForm(instance=intention) # An unbound form
这篇关于对象没有属性'get'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!