Django文件共享应用程序(向用户提供链接的问题)

时间:2017-10-09 09:46:34

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

我正在创建文件共享应用。我可以上传文件,但无法向用户提供下载链接。

请查看以下视图,

def user_in(request):

    if not request.user.is_authenticated:
        return render(request, 'accounts/logout.html')

    else:
        if request.method == 'POST':
            form_new = Fileupload(request.POST, request.FILES )
            #instance=form_new.save(commit=False)
            #instance.save()
            if form_new.is_valid():
                instance = form_new.save(commit=False)
                instance.user_uploaded=request.user
                instance.save()
                return redirect('in')
        else:
            form_new = Fileupload()
            data = user_files.objects.filter(user_uploaded=request.user)
            args = {'form_new': form_new, 'data': data}#, 'url': form_new.get_absolute_url()}
            return render(request, 'accounts/in.html', args)

和model.py是,

class user_files(models.Model):
    Filename = models.CharField(max_length=50)
    Browse = models.FileField(upload_to='img/')
    user_uploaded = models.CharField(max_length=50, blank=True)

模板是,

<form action="." method="POST" enctype="multipart/form-data" >
    <h3>Welcome to DropBox<br><br></h3>

    {% csrf_token %}
    {{form_new.as_p}}

    <p><input type="submit" value="Save" ></p>
    <br>
    <a href="{% url 'logout' %}">Logout</a>

    {%else%}
    <p>You must login first</p>
    <a href="{% url 'login' %}">Logout</a>
    {% endif %}
<br><br>
S
    <a href="{% url 'share' %}">Share Files</a>
    <br><br>

    User is : {{request.user}}

    {% for da in data %}
    <h3>{{da.Filename}} {{da.Browse}} </h3>
    {% endfor %}

</form>

请你指导我如何为上传的链接提供下载链接,我现在卡住了。

提前感谢。

1 个答案:

答案 0 :(得分:0)

根据documentation,您可以使用.url获取网址:

user_files_instance.browse.url

希望它有所帮助!