Django为用户上传文件

Django为用户上传文件,django,Django,我可以用django上传文件。我还设法注册了不同的用户。 但是当用户登录时,他们都会看到任何人上传的文件集。 相反,我希望用户只查看他/她上传的文件集 这是我的模特 class Document(models.Model): docfile = models.FileField(upload_to='documents/%Y/%m/%d') views.py def list(request): if request.method == 'POST': for

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

这是我的模特

class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
views.py

def list(request):

    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 HttpResponse("File Uploaded.. Kindly Refresh!!")
    else:
        form = DocumentForm() # A 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(
        'login/list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )
LIST.html

 <table cellpadding=20>
    {% for document in documents %}
        <tr>
            <td><a href="{{ document.docfile.url }}">{{ document.docfile.name }}</a> </td>
            <td></td>
            <td></td>
            <td><a href="delete/{{ document.docfile.name }}"> Delete</a></td>

        </tr>
    {% endfor %}
    </table>

  {% else %}
    <p>No documents.</p>
  {% endif %}

{文档%中的文档为%}
{%endfor%}
{%else%}
没有文件

{%endif%}
将models.py更改为此

from django.contrib.auth.models import User
class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    user = models.ForeignKey(User)
将views.py更改为此

def list(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.user = request.user
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponse("File Uploaded.. Kindly Refresh!!")
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.filter(user=request.user)

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

将models.py更改为此

from django.contrib.auth.models import User
class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    user = models.ForeignKey(User)
将views.py更改为此

def list(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.user = request.user
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponse("File Uploaded.. Kindly Refresh!!")
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.filter(user=request.user)

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

用户
字段添加到您的
文档
模型中,然后在视图中对该字段进行筛选实际上,我在views.py中设置了所有者,它设置了所有者,但现在我面临的问题是如何在LIST.html中为特定用户筛选结果。请提供帮助。它根据登录的用户存储文件。现在,我需要做的就是过滤每个特定用户的结果。请提供任何帮助。当前用户是
请求。用户
,根据它筛选
文档
,您已经完成了,例如
文档=文档.对象.筛选(用户=请求.用户)
。这就是@J.Ghyllebert建议将
用户
字段添加到
文档
…将
用户
字段添加到
文档
模型的原因,然后在视图中对该字段进行筛选实际上,我已经在views.py中设置了所有者,但现在我面临的问题是如何在LIST.html中为特定用户过滤结果。请提供帮助。它根据登录的用户存储文件。现在,我需要做的就是过滤每个特定用户的结果。请提供任何帮助。当前用户是
请求。用户
,根据它筛选
文档
,您已经完成了,例如
文档=文档.对象.筛选(用户=请求.用户)
。这就是@J.Ghyllebert建议在
Document
中添加
user
字段的原因。…
user=models.ForeignKey(user,on\u delete=models.CASCADE)
对于
Django版本3.0.4
user=models.ForeignKey(user,on\u delete=models.CASCADE)
对于
Django版本3.0.4