python的django寫頁面上傳文件及遇到的問題小結(jié)
首先上結(jié)構(gòu)
mynode -> app5 -> urls.py & views.py | -> templates -> 5 -> upload.html | -> mynode -> urls.py | -> media
按照順序,先上app5/urls.py
from django.urls import path from app5 import views as v5 app_name = 'app5' urlpatterns = [ path('upload_file/', v5.upload_file, name = 'upload_file'), path('show_upload/', v5.show_upload, name = 'show_upload'), ]
path('upload_file/', v5.upload_file, name = 'upload_file'),指定upload_file跳轉(zhuǎn)功能
path('show_upload/', v5.show_upload, name = 'show_upload'),指定show_upload跳轉(zhuǎn)功能
接著是app5/view.py
from django.shortcuts import render from django.http import HttpResponse import os def show_upload(request): return render(request, '5/upload.html') def upload_file(request):if request.method == 'POST': get_file = request.FILES.get('myfile',None) if get_file: path = 'media/uploads' if not os.path.exists(path): os.makedirs(path) dest = open(os.path.join(path,get_file.name),'wb+') for chunk in get_file: dest.write(chunk) dest.close() return HttpResponse('上傳文件成功!') else: return HttpResponse('沒有上傳文件!')
首先寫了一個(gè)show_upload方法,跳轉(zhuǎn)到初始頁面
接下來是upload_file方法,首先判斷請(qǐng)求方式是否是POST,接下來獲取上傳文件,指定上傳路徑,如果路徑不存在就創(chuàng)建一個(gè),把上傳文件內(nèi)容寫到指定路徑下
再來是templates/5/upload.html
<!--<from enctype="multipart/form-data" action="{% url 'app5:upload_file' %}" method="post">--> //這個(gè)是錯(cuò)誤的 <form enctype="multipart/form-data" action="{% url 'app5:upload_file' %}" method="post"> {% csrf_token %} <input type="file" name="myfile" /> <br/> <input type="submit" value="upload_file" /> </form> <!--</from>--> //這個(gè)是錯(cuò)誤的
指定了一個(gè)action,{% url 'app5:upload_file' %},app5是app5/urls.py中的app_name,upload_file則是要跳轉(zhuǎn)連接,同時(shí)因?yàn)閡rl已經(jīng)指定這個(gè)連接要跳轉(zhuǎn)的views中的功能,因此這個(gè)就是app5/view.py里面的upload_file方法
這個(gè)頁面展示是正常的,但是在寫好功能以后,無論怎么點(diǎn)提交,都沒法跳轉(zhuǎn)到upload_file功能
仔細(xì)看表單的名稱<from,這個(gè)坑我踩了好久,后來仔細(xì)看了下,應(yīng)該是form,改了之后就好使了
最后是mynode/urls.py
from django.contrib import adminfrom django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('app5/', include('app5.urls')), ]
指定app5跳轉(zhuǎn)到app5/urls.py
最后打開瀏覽器,輸入鏈接http://localhost:8000/app5/show_upload/
選擇要上傳的文件,點(diǎn)擊upload_file按鈕
這里跳轉(zhuǎn)到upload_file路徑,并且顯示上傳文件成功
到此這篇關(guān)于python的django寫頁面上傳文件以及遇到的問題的文章就介紹到這了,更多相關(guān)python django上傳文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中關(guān)于使用模塊的基礎(chǔ)知識(shí)
這篇文章主要介紹了Python中關(guān)于使用模塊的基礎(chǔ)知識(shí),是Python入門的基礎(chǔ),需要的朋友可以參考下2015-05-05PyQt5 PySide2 觸摸測試功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了PyQt5 PySide2 觸摸測試功能的實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04詳解Anconda環(huán)境下載python包的教程(圖形界面+命令行+pycharm安裝)
這篇文章主要介紹了Anconda環(huán)境下載python包的教程(圖形界面+命令行+pycharm安裝),這篇文章很適合小白入手級(jí)別的,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11python新手學(xué)習(xí)可變和不可變對(duì)象
在本篇文章里小編給大家分享了是一篇關(guān)于python可變對(duì)象和不可變對(duì)象的基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。2020-06-06Python光學(xué)仿真wxpython透鏡演示系統(tǒng)初始化與參數(shù)調(diào)節(jié)
這篇文章主要為大家介紹了Python光學(xué)仿真wxpython透鏡演示系統(tǒng)的初始化與參數(shù)調(diào)節(jié),同樣在學(xué)習(xí)wxpython透鏡演示系統(tǒng)的入門同學(xué)可以借鑒參考下,希望能夠有所幫助2021-10-10python切片復(fù)制列表的知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python切片復(fù)制列表的知識(shí)點(diǎn)相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10