Django为用户上传文件历史记录

时间:2018-04-06 08:52:43

标签: python django django-models django-forms django-views

我可以上传django中的文件。我还设法注册了不同的用户。但是当用户登录时,他们都会看到任何人上传的文件集。相反,我希望用户只查看他/她上传的那些文件集。

models.py

from django.contrib.auth.models import User

class Document(models.Model):
    user = models.ForeignKey(User)
    description = models.CharField(max_length=255, blank=True)
    document = models.CharField(max_length=255, )
    uploaded_at = models.DateTimeField(auto_now_add=True)

views.py

def fileupload(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        document = Document(
        description=request.POST['description'],
        document=myfile.name, 
        uploaded_at=datetime.datetime.now(),)
        document.save()
        messages.success(request, 'Member was created successfully!')
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        return redirect('fileupload')
    else:
        documents = Document.objects.order_by('-uploaded_at')[:3]
        context = {'documents': documents}
    return render(request, 'fileupload.html', context)

0 个答案:

没有答案