Python Django:AttributeError - 对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4777482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:28:05  来源:igfitidea点击:

Django: AttributeError - Object has no attribute

pythondjangodjango-formsdjango-views

提问by Emile

This view is throwing the AttributeError saying :'PhotoForm' object has no attribute 'reservation'. What happens- the function passes, but doesn't actually upload the image. In debugging it, the form is not valid.

此视图抛出 AttributeError 说:'PhotoForm' 对象没有属性 'reservation'。会发生什么 - 函数通过,但实际上并没有上传图像。在调试它时,表单无效。

I tried printing

我试过打印

  • form.reservation
  • form.message
  • form.photo
  • 形式保留
  • 表单消息
  • 表格.照片

But that shows this traceback ( which is the reason why the form isn't valid & not completing the function.

但这显示了这种回溯(这就是表单无效且未完成功能的原因。

As I built this off other functions that are working, I'm a bit confused. Thanks for the help!

当我根据其他正在运行的函数构建它时,我有点困惑。谢谢您的帮助!

The HTML Form

HTML 表单

<form action="/photo/new/" method="post">{% csrf_token %}
    <dl>
        <dt>{{ form.reservation.label }}</dt>
            <dd><select name="reservation"> {% for reservation in reservations %} <option value="{{reservation.id}}">{{reservation.date}} {{reservation.chef.cook.get_profile.firstname}} - {{reservation.guest.get_profile.firstname}}</option>{% endfor %}</select></dd>
        <dt>{{ form.photo.label }}</dt>
            <dd>{{ form.photo }}</dd>
        <dt> {{ form.message.label }}</dt>
            <dd>{{ form.message }}</dd>
    </dl>
<button type="submit">Submit</button>
</form>

The View

风景

@login_required
def new_photo(request, template_name="photo/newphoto.html"):

  meals = Reservation.objects.filter(guest=request.user.id)
  form = PhotoForm(request.POST)
  form.data = { "reservations": meals }
  if request.method == 'POST':
    form.photographer = request.user
    form.data.get('reservation')
    print form.reservation
    print form.message
    print form.photo
    if form.is_valid():
      print" hellos"
      save_reservation_photo(request.FILES["photo"])
      photo = form.save(commit=False)
      photo.photographer = request.user
      photo.save()
      return HttpResponseRedirect('/photo/%d/' % photo.id )
  else:
    form = PhotoForm()

  data = {'form':form,'reservations':meals,
    'add':True
  }

  return render_to_response(template_name,
                            data,
                            context_instance=RequestContext(request))

The Form:

表格:

class PhotoForm(forms.ModelForm):
  class Meta:
    model = Photo
    fields = ('reservation','photo','message')

  def __init__(self, *args, **kwargs):
      super(PhotoForm, self).__init__(*args, **kwargs)

The Model:

该模型:

class Photo(models.Model):

  photographer = models.ForeignKey(User)
  pub_date = models.DateTimeField(default=datetime.now,auto_now_add=True,db_index=True)
  reservation = models.ForeignKey(Reservation)

  message = models.CharField(default='',max_length=140)

  photo = models.ImageField(default='',upload_to="reservation_images/")

The traceback

回溯

Traceback:
Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/photo/new/
Django Version: 1.2.3
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.comments',
 'django.contrib.markup',
 'django.contrib.sitemaps',
 'lib.debug_toolbar',
 'src',
 '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',
 'lib.debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/photo/new/
Django Version: 1.2.3
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.comments',
 'django.contrib.markup',
 'django.contrib.sitemaps',
 'lib.debug_toolbar',
 'src',
 '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',
 'lib.debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/Users/emilepetrone/Sites/meal/../meal/src/views.py" in new_photo
  532.     print form.reservation

Exception Type: AttributeError at /photo/new/
Exception Value: 'PhotoForm' object has no attribute 'reservation'

采纳答案by Emile

The key was having files=request.FILES in the form.

关键是在表单中有 files=request.FILES。

@login_required
def new_photo(request, template_name="photo/newphoto.html"):

  form = PhotoForm( user= request.user, data=request.POST, files=request.FILES)

  if request.method == 'POST':
    form.photographer = request.user
    if form.is_valid():
      photo = form.save(commit=False)
      photo.photographer = request.user
      photo.save()
      return HttpResponseRedirect('/photo/%d/' % photo.id )

回答by Paulo Scardine

should not forms (with image or file fields) be initialized with request.FILES?

表单(带有图像或文件字段)不应该用 request.FILES 初始化吗?

form = PhotoForm(request.POST, request.FILES)

回答by slacy

Your inbound form processing looks a bit odd to me, but I usually don't use ModelForm. Here's what mine generally look like:

您的入站表单处理在我看来有点奇怪,但我通常不使用 ModelForm。这是我的一般情况:

def some_form_view(request): 
  if request.method != 'POST': 
    raise Http404  # or whatever 
  form_data = FormClass(request.POST, request.FILES)
  if not form_data.is_valid():
    context['form'] = form_data 
    return render_to_response(...)  # invalid for m

  new_object = ModelObject(
      field=form_data.cleaned_data['field'], 
      other_field=form_data.cleaned_data['other_field'])
  new_object.save()

  context['new_object'] = new_object
  return render_to_response(...)  # success 

So things to note:

所以要注意的事项:

  • I don't think "form.reservation" is a valid identifier. You should just say "print form" or "print str(form)" and not try to print the individual fields.

  • You should almost certainly be calling is_valid() before doing anything with that data. That should/will raise the form validation errors if the form itself is invalid.

  • Only ever access the form data directly through form.cleaned_data['foo'] and never via .data, because you're accessing something potentially dangerous coming in from the user.

  • 我不认为“form.reservation”是一个有效的标识符。您应该只说“打印表单”或“打印 str(form)”,而不要尝试打印各个字段。

  • 在对该数据执行任何操作之前,您几乎肯定应该调用 is_valid()。如果表单本身无效,那应该/将引发表单验证错误。

  • 只能通过 form.cleaned_data['foo'] 直接访问表单数据,而不要通过 .data,因为您正在访问来自用户的潜在危险内容。

Hope this helps.

希望这可以帮助。