如何在 Django 2 中上传任何类型的文件

How to Upload any kind of file in django 2

我是 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>

我可以保存所有内容但我需要保存文件路径并将文件保存到本地。我阅读了他们的文档,但无法帮助我。请帮我解决这个问题

您的 Model 中的 FileField,除了其他字段外,应该如下所示:

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 定义为该目录的基础 public 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 中,您可以使用文件对象获取任何文件,例如:

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)