我的目标是让用户将文件上传到用户特定的文件夹。
我得到的错误是
no such column: notendur_document.user_id
以下是我的views.py
文件的相关部分。这是上传的地方。
@login_required
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('notendur.views.list'))
else:
form = DocumentForm() # An empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'notendur/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
这是我的models.py
文件:
def _upload_path(instance,filename):
return instance.get_upload_path(filename)
class Document(models.Model):
docfile = models.FileField(upload_to=_upload_path)
user = models.ForeignKey(User)
def get_upload_path(self,filename):
return "media/uploads/"+str(self.user.id) + "/" + '%Y.%m.%d' + filename
根据相关的.html
文件,错误发生在documents
的{{1}}变量中。
在list()
注册用户方法:
views.py
答案 0 :(得分:0)
只需传递pk = True in theforeingkey 例如用户=模型.ForeignKey(User,pk = True) 这将解决问题