Django 用户错误:"AttributeError: 'User'对象没有属性“get”; ”

发布于 2024-08-15 06:19:26 字数 2662 浏览 4 评论 0 原文

我 50 小时前刚刚开始使用 Django 和 Python ;-) 所以这可能很简单,但我陷入了困境。

我在我的项目中使用类似的“创建”视图和类似的表单实现,没有任何问题。在这种情况下,我收到上述错误。

我用的是Pinax...

2   from django.shortcuts import render_to_response, get_object_or_404
3   from django.template import RequestContext
4   from django.http import HttpResponseRedirect
5   from django.core.urlresolvers import reverse
6   from django.contrib.auth.models import User
7   from django.contrib.auth.decorators import login_required
8   from django.utils.translation import ugettext_lazy as _
9   
10  from django.conf import settings
11  
12  if "notification" in settings.INSTALLED_APPS:
13      from notification import models as notification
14  else:
15      notification = None
16  
17  from location.models import Location
18  from location.forms import LocationForm, LocationUpdateForm
19  
20  
21  @login_required
22  def create(request, form_class=LocationForm, template_name="location/create.html"):
23      location_form = form_class(request.user, request.POST or None)
24  
25  
26      
27      if location_form.is_valid():
28          location = location_form.save(commit=False)
29          location.creator = request.user
30          location.save()
31          return HttpResponseRedirect(location.get_absolute_url())
32      
33      return render_to_response(template_name, {
34          "location_form": location_form,
35      }, context_instance=RequestContext(request))

Traceback (most recent call last):
  File "/Users/philgo20/code/LeadMiner/django/core/servers/basehttp.py", line 636, in __call__

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/wsgi.py", line 241, in __call__

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/base.py", line 134, in get_response

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/base.py", line 154, in handle_uncaught_exception

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/base.py", line 92, in get_response

  File "/Users/philgo20/python/django/trunk/django/contrib/auth/decorators.py", line 78, in __call__
    return self.view_func(request, *args, **kwargs)
  File "/Users/philgo20/code/jezam_kms/apps/location/views.py", line 27, in create
    if location_form.is_valid():
  File "/Users/philgo20/code/LeadMiner/django/forms/forms.py", line 120, in is_valid

  File "/Users/philgo20/code/LeadMiner/django/forms/forms.py", line 111, in _get_errors

  File "/Users/philgo20/code/LeadMiner/django/forms/forms.py", line 234, in full_clean

  File "/Users/philgo20/code/LeadMiner/django/forms/widgets.py", line 170, in value_from_datadict

AttributeError: 'User' object has no attribute 'get'

I've just started Django and Python 50 hours ago ;-) so this might be an easy one but I am stuck.

I am using similar 'create' view with similar Form implementation in my project with no problem. In this case, I get the above mentionned error.

I am using Pinax...

2   from django.shortcuts import render_to_response, get_object_or_404
3   from django.template import RequestContext
4   from django.http import HttpResponseRedirect
5   from django.core.urlresolvers import reverse
6   from django.contrib.auth.models import User
7   from django.contrib.auth.decorators import login_required
8   from django.utils.translation import ugettext_lazy as _
9   
10  from django.conf import settings
11  
12  if "notification" in settings.INSTALLED_APPS:
13      from notification import models as notification
14  else:
15      notification = None
16  
17  from location.models import Location
18  from location.forms import LocationForm, LocationUpdateForm
19  
20  
21  @login_required
22  def create(request, form_class=LocationForm, template_name="location/create.html"):
23      location_form = form_class(request.user, request.POST or None)
24  
25  
26      
27      if location_form.is_valid():
28          location = location_form.save(commit=False)
29          location.creator = request.user
30          location.save()
31          return HttpResponseRedirect(location.get_absolute_url())
32      
33      return render_to_response(template_name, {
34          "location_form": location_form,
35      }, context_instance=RequestContext(request))

Traceback (most recent call last):
  File "/Users/philgo20/code/LeadMiner/django/core/servers/basehttp.py", line 636, in __call__

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/wsgi.py", line 241, in __call__

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/base.py", line 134, in get_response

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/base.py", line 154, in handle_uncaught_exception

  File "/Users/philgo20/code/LeadMiner/django/core/handlers/base.py", line 92, in get_response

  File "/Users/philgo20/python/django/trunk/django/contrib/auth/decorators.py", line 78, in __call__
    return self.view_func(request, *args, **kwargs)
  File "/Users/philgo20/code/jezam_kms/apps/location/views.py", line 27, in create
    if location_form.is_valid():
  File "/Users/philgo20/code/LeadMiner/django/forms/forms.py", line 120, in is_valid

  File "/Users/philgo20/code/LeadMiner/django/forms/forms.py", line 111, in _get_errors

  File "/Users/philgo20/code/LeadMiner/django/forms/forms.py", line 234, in full_clean

  File "/Users/philgo20/code/LeadMiner/django/forms/widgets.py", line 170, in value_from_datadict

AttributeError: 'User' object has no attribute 'get'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2024-08-22 06:19:26

views.py 的第 23 行,您将 request.user 作为第一个参数传递给 LocationForm 的实例化。

第一个参数应为 request.POST ,除非您的表单已覆盖 __init__() 方法。

On line 23 of your views.py you are passing request.user as the first parameter to the instantiation of LocationForm.

The first parameter should be request.POST unless your form has overridden the __init__() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文