微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Django文件未上传在打印时获取<MultiValueDict:{}> Django 3.1

如何解决Django文件未上传在打印时获取<MultiValueDict:{}> Django 3.1

我尝试使用Django上传文件,但无法执行。有人可以告诉我此代码文件出了什么问题吗? 所有代码文件在这里

views.py

@method_decorator(login_required,name='dispatch')
class CreatePost(View):

    def post(self,request,boardId):
        postTitle = request.POST.get('postTitle')
        postDescription = request.POST.get('postDescription')
        print(postTitle )
        print(postDescription)
        createdOn = datetime.Now()
        createdBy = request.user
        boardId = BoardModel.objects.get(boardId = boardId)

        newPost = Post(
                postTitle = postTitle,postDescription = postDescription,createdOn = createdOn,createdBy = createdBy,boardId = boardId,)
        newPost.save()

        postId = newPost.postId
        request_file = request.FILES['postAttachment'] if 'postAttachment' in request.FILES else None
        print(request.FILES)
        if request_file:
            print(request.POST['postAttachment'])
            storeAt = settings.STATIC_DIR
            fs = FileSystemStorage(storeAt)
            filename = request.POST['postAttachment']
            file = fs.save(filename,request_file)

        print("boardId in create post" + str(boardId))

        return redirect(request.Meta['HTTP_REFERER'])

模板

<form action="{% url 'create_post' boardId=board.boardId %}" method="POST" enctype="multiple/form-data">

{% csrf_token %}
<input type="text" name="postTitle" id="id_postTitle" placeholder="Enter The Post Title" class="form-control form-input" required>
<be>
<textarea name="postDescription" id="postForm" cols="3" class="form-control" style="resize: vertical; height: 100px;" placeholder="Enter Post Description" cols="5" required></textarea>
<br>
<span id="attachedFilesList" class="row"></span> 
<br>
<button type="submit" class="btn btn-login btn-green" id="postBtn"><span>Post</span></button>
<input type="file" name="postAttachment" id="fileUpload" onchange="previewFiles(event);">
&nbsp;&nbsp;

在打印请求时。文件我得到了

输出(cmd):

C:\Users\PATEL JANVI\Envs\post\lib\site-packages\django\db\models\fields\__init__.py:1370: RuntimeWarning: DateTimeField Post.createdOn received a naive datetime (2020-08-13 22:22:53.036403) while time zone support is active.
RuntimeWarning)        
<MultiValueDict: {}>                                                                   
boardId in create postDjango                                                      
[13/Aug/2020 22:22:53] "POST /post/create_post/10 HTTP/1.1" 302 0                                
boardId:  10                                                                            
[13/Aug/2020 22:22:53] "GET /board/view_board/ HTTP/1.1" 200 34671               
C:\Users\PATEL JANVI\Desktop\Add_Post\si\post\views.py changed,reloading.             
Watching for file changes with StatReloader                                          
Performing system checks...                                                         

我得到了除文件附件以外的所有其他值。谁能帮我解决这个问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。