如何使用Django 2

时间:2019-01-25 05:11:55

标签: django file-upload django-file-upload

我是Django 2的新手。我试图在Django中上传文件,这是我的代码

View.py

def addBook(request):
    checkName = AddBook.objects.filter(title=request.POST.get('title'))
    if not checkName:
        bookAdd = AddBook(
            title=request.POST.get('title'),
            slug=slugify(request.POST.get('title')),
            description=request.POST.get('description'),
            cover_image=request.FILES.get('cover_image'),
            file=request.FILES.get('file'),
            category=request.POST.get('category'),
            created_by=request.user.id,
        )
        bookAdd.save()
        messages.add_message(request, messages.INFO, 'Book Saved Successfully')
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    else:
        messages.add_message(request, messages.INFO, 'Book Title Already Exists')
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

根据评论更新

这是我的模板代码 bookSave.html

<form action="{% url 'addBook' %}" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="modal-body">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs 12">
                            <div class="form-group ic-cmp-int">
                                <div class="form-ic-cmp">
                                    <i class="notika-icon notika-edit"></i>
                                </div>
                                <div class="nk-int-st">
                                    <input type="text" class="form-control input-sm" required="required" name="title"
                                           Placeholder="Title">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-body">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            <div class="form-group ic-cmp-int">
                                <div class="form-ic-cmp">
                                    <i class="notika-icon notika-mail"></i>
                                </div>
                                <div class="nk-int-st">
                                <textarea class="form-control input-sm" required="required" name="description"
                                          placeholder="Description"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-body">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            <div class="form-group ic-cmp-int">
                                <div class="form-ic-cmp">
                                    <i class="notika-icon notika-dollar"></i>
                                </div>
                                <div class="nk-int-st">
                                    <input type="file" name="cover_image" required="required" class="form-control input-sm">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-body">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            <div class="form-group ic-cmp-int">
                                <div class="form-ic-cmp">
                                    <i class="notika-icon notika-house"></i>
                                </div>
                                <div class="nk-int-st">
                                    <input type="file" name="file" required="required" class="form-control input-sm">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-body">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            <div class="form-group ic-cmp-int">
                                <div class="form-ic-cmp">
                                    <i class="notika-icon notika-next"></i>
                                </div>
                                <div class="nk-int-st">
                                    <input type="text" name="category" required="required" class="form-control input-sm"
                                           Placeholder="Category">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-default">Save changes</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </form>

我可以保存所有内容,但是我需要保存文件路径并在本地保存文件。我阅读了他们的文档,但帮不上我的忙。请帮我解决这个问题

1 个答案:

答案 0 :(得分:3)

模型中的文件字段,除其他字段外,应如下所示:

class AddBook(models.Model):
    # file will be uploaded to MEDIA_ROOT/uploads
    file = models.FileField(upload_to='uploads/')
    # or...
    # file will be saved to MEDIA_ROOT/uploads/2015/01/30
    file = models.FileField(upload_to='uploads/%Y/%m/%d/')

在设置文件中,您需要将 MEDIA_ROOT 定义为您希望Django存储上载文件的目录的完整路径。 (为了提高性能,这些文件未存储在数据库中。)将MEDIA_URL定义为该目录的基本公共URL。确保该目录可由Web服务器的用户帐户写入。

在settings.py中,您应该设置例如:

MEDIA_ROOT = '/home/foo/bar/yourproject/assets'

您可能还想研究和设置Django中的静态文件存储(“ static”文件夹除外)。

https://docs.djangoproject.com/en/2.0/howto/static-files/#configuring-static-files

在Django中,您可以使用File Object获取任何文件,例如:

from django.core.files import File

# Open an existing file using Python's built-in open()
f = open('/path/to/mybookfile.pdf')
myfile = File(f)