2

我无法上传文件。我正进入(状态

Type error builtin_function_or_method' object is not iterable

模型.py

class seeker(models.Model):
    user = models.OneToOneField(User)
    birthday = models.DateField()

class Upload(models.Model):
    user = models.ForeignKey(Seekers)
    resume = models.FileField(upload_to ='resume', blank = True, null = True)

表格.py

class SeekersForm(forms.Form):
    resume = forms.FileField(label = 'Select a file',help_text = 'max.3 MB')

视图.py

def List(request):

    # Handle file upload
    if request.method == 'POST':
        form = SeekersForm(request.POST, request.FILES)
        if form.is_valid():

            #id = User.object.get(id)
            newdoc = Seekers.objects.get(user_id)
            newdoc.resume =Upload(resume = request.FILES['resume'])
            newdoc.save()

            #seekers_edit = Seekers.objects.get(id) 
            #seekers_edit.resume = Seekers(resume = request.FILES['resume'])
            #seekers_edit.save()

            #Redirect to the document list after POST
            return HttpResponseRedirect('/profile/')
    else:
        form = SeekersForm() # A empty, unbound form

    #Load documents for the list page   
    seekers = Seekers.objects.all()

    #Render list page with the documents and the form
    return render_to_response('list.html',{'seekers':seekers,'form':form},context_instance=RequestContext(request))
4

2 回答 2

0

您的代码中的问题:

  • 您的表单定义复制了模型中的信息 - 只需使用forms.ModelForm(withexclude以免显示该user字段)
  • 如当前粘贴的那样,newdoc = Seekers.objects.get(user_id)将引发TypeError( 'foo' object is not iterable); .get()接受关键字参数过滤器,而不是其他任何东西。
  • request.FILES['resume']不需要或不建议手动访问

所以,简而言之,你就快到了;让 Django 表单为您完成更多工作:

# forms.py
class SeekerForm(forms.ModelForm)
    class Meta:
        model = Seeker

# views.py    
def seeker_list(request):
    # Opinions are divided as to whether it's ever appropriate to 
    # modify the database like this on a GET request, but it seems
    # to make sense here
    seeker = Seekers.objects.get_or_create(user=request.user)
    if request.method == 'POST':
        form = SeekerForm(request.POST, request.FILES, instance=seeker)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/profile/')
    else:
        form = SeekerForm(instance=seeker)

    seekers = Seekers.objects.all()

    #Render list page with the documents and the form
    return render_to_response('list.html', {
        'seekers':seekers,
        'form':form
    }, context_instance=RequestContext(request))

不清楚代码中注释掉的部分的意义(如果有的话)是什么——我假设你总是想修改当前用户的Seeker,但如果不是,则酌情调整。

于 2012-08-22T17:15:14.277 回答
0

很难说你的问题出在哪里,但我认为以下代码行是主要问题:

newdoc.resume =Upload(resume = request.FILES['resume'])

在保存整个模型实例之前,您必须将文件显式保存在 FileField 中。此外,如果您的一个模型中有一个 ForeignKey 字段,并且您想为其分配另一个模型的实例,请在进行分配之前先保存该实例。在不知道你的Seekers模型的情况下,我所能做的就是猜测什么可能对你有帮助。类似以下内容可能会帮助您入门:

your_file = request.FILES['resume']
upload_instance = Upload()
upload_instance.resume.save(name=your_file.name, content=your_file, save=False)
upload_instance.user = ... # Here goes an instance of your Seekers model
upload_instance.save() # Here you save the whole instance of your Upload model

另外,请注意以下事项:

  1. 您的模型Seekers应该Seeker使用单数而不是复数来命名。这通常应该与您的所有模型一样。
  2. Python 函数应始终以小写字母开头,即,list而不是List. 然而,无论如何,这个名字在这里是一个不好的选择,因为listPython 的标准库中已经存在一个被调用的函数。

请仔细查看 Django 的文档。你需要知道的都在里面。我建议您特别阅读以下部分:

https://docs.djangoproject.com/en/1.4/ref/models/fields/#filefield

https://docs.djangoproject.com/en/1.4/ref/files/file/

于 2012-08-22T16:48:36.357 回答