问题描述
我尝试使用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);">
在打印请求时。文件我得到了
输出(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 (将#修改为@)