2012-07-26 317 views
31

我無法找到任何解決方案與我的問題類似的錯誤。對象沒有屬性'get'

的錯誤是,當我嘗試編輯對象:

'意向' 對象有沒有屬性 '得到'

我有符合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 %} 

控制器代碼:

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) 
          ) 

有人可以給我任何建議嗎?

引用:

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' 
+1

是的。發佈完整的追溯。 – 2012-07-26 11:06:41

+0

我編輯問題 – keram 2012-07-26 11:48:04

+0

-1沒有評論爲什麼? – keram 2012-07-26 11:48:14

回答

59

你的問題是在這裏:

intention = Intention.objects.get(pk=id) 
form = IntentionForm(intention) # An unbound form 

的第一個參數的形式是數據,但你逝去的實例。要正確地傳遞情況下,你應該使用:

intention = Intention.objects.get(pk=id) 
form = IntentionForm(instance=intention) # An unbound form 
+3

我怎麼可能錯過了,我讀了10次文檔https://docs.djangoproject.com/zh/dev/topics/forms/modelforms/ – max4ever 2014-07-14 14:09:38

15

上述答案是正確的,但是,也可以通過正確地傳遞參數的形式,這是用於管理員模式的初始化產生這個錯誤。

實施例:

class MyForm(forms.ModelForm): 
    def __init__(self, *args, **kwargs): 
    super(MyForm, self).__init__(self, *args, **kwargs) 

通知自的雙重通過?它應該是:

class MyForm(forms.ModelForm): 
    def __init__(self, *args, **kwargs): 
    super(MyForm, self).__init__(*args, **kwargs) 
+6

住你多久的人。謝謝。我與主持人的論點有類似的錯字。 – Jay 2017-01-11 19:38:38

+3

我剛剛搜索了幾個小時找到這個答案,暗示我有類似的問題。謝謝一堆! – 2017-01-14 00:48:42

+1

這是我的情況,謝謝你! – Antiphon0x 2017-08-15 09:59:02

1

在Django中,要小​​心如何命名您的視圖和模型。

在我的情況,我在models.py了這一點:

class Contact(models.Model): 
    ... 

views.py,我有:

def contact(request): 
    ... 

然後,在urls.py,我有:

from .views import Contact 

所以,我實際上是在導入模型類,而不是聯繫函數,所以我的錯誤是s:

'Contact' object has no attribute 'get' 

該對象沒有屬性get。這假設來自views.py,而不是model