Django中文件上傳和文件訪問微項(xiàng)目的方法
Django中上傳文件方式。
如何實(shí)現(xiàn)文件上傳功能?
1創(chuàng)建項(xiàng)目uploadfile:
創(chuàng)建app:front
項(xiàng)目設(shè)置INSTALLED_APPS中添加'front'
INSTALLED_APPS = [ ''' 'front' ]
#后面添加MEDIA_ROOT和MEDIA_URL
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR,'static') MEDIA_ROOT = os.path.join(BASE_DIR,'media') MEDIA_URL = '/media/'
2.models,views都寫用front文件夾里面。
modes.py創(chuàng)建代碼。
class Article(models.Model): '''創(chuàng)建個文章表格,測試上傳文件''' title = models.CharField(max_length=100,unique=True) content = models.CharField(max_length=100) articlefile = models.FileField(upload_to='%Y/%m/%d',unique=True) #這里upload_to='%Y/%m/%d'可以先不設(shè)置,設(shè)置的目的是上傳文件保存在media目錄下時,自動創(chuàng)建以時間為標(biāo)記文件層次文件夾目錄
使用命令
makemigrations,和migrates進(jìn)行遷移
打開db.sqlite3可以看到遷移成功后的數(shù)據(jù)表front_article
數(shù)據(jù)庫中有article表,說明遷移成功。
3.寫視圖
from django.shortcuts import render,HttpResponse from django.views.generic import View from .models import Article # Create your views here. class UploadFile(View): def get(self,request): contents = Article.objects.all() return render(request,'index.html',locals()) def post(self,request): title = request.POST.get('title') content = request.POST.get('content') file = request.FILES.get('myfile') Article.objects.create(title=title,content=content,articlefile=file) return HttpResponse("成功")
這里使用類視圖
4創(chuàng)建index模板。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% for content in contents %} <li>標(biāo)題:{{ content.title }}</li> <li>內(nèi)容:{{ content.content }}</li> <a href="{% url 'index' %}media/{{ content.articlefile }}" rel="external nofollow" ><li>{{ content.articlefile }}</li></a> {% endfor %} {#for循環(huán)主要顯示數(shù)據(jù)圖中數(shù)據(jù)。有標(biāo)題,有內(nèi)容和文件鏈接#} <form action="" method="post" enctype="multipart/form-data" > <input type="text" name="title" > <input type="text" name="content"> <input type="file" name="myfile" > <input type="submit" value="提交"> </form> </body> </html>
顯示效果如下:
5關(guān)鍵性一步
urls.py
from django.urls import path from front import views from django.conf.urls.static import static from django.conf import settings urlpatterns = [ path('',views.UploadFile.as_view(),name='index'), ]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
使用static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)可以直接訪問文件。非常方便。
到此這篇關(guān)于Django中文件上傳和文件訪問微項(xiàng)目的方法的文章就介紹到這了,更多相關(guān)django上傳文件和文件訪問微項(xiàng)目內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Django 如何實(shí)現(xiàn)文件上傳下載
- Django和Ueditor自定義存儲上傳文件的文件名
- 詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式
- 基于django和dropzone.js實(shí)現(xiàn)上傳文件
- python中Django文件上傳方法詳解
- Django后端分離 使用element-ui文件上傳方式
- Django Admin 上傳文件到七牛云的示例代碼
- Django實(shí)現(xiàn)任意文件上傳(最簡單的方法)
- Django 解決上傳文件時,request.FILES為空的問題
- django 文件上傳功能的相關(guān)實(shí)例代碼(簡單易懂)
- django上傳文件的三種方式
相關(guān)文章
python實(shí)現(xiàn)任意位置文件分割的實(shí)例
今天小編就為大家分享一篇python實(shí)現(xiàn)任意位置文件分割的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12TensorFlow 多元函數(shù)的極值實(shí)例
今天小編就為大家分享一篇TensorFlow 多元函數(shù)的極值實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python函數(shù)中將變量名轉(zhuǎn)換成字符串實(shí)例
這篇文章主要介紹了python函數(shù)中將變量名轉(zhuǎn)換成字符串實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Mac中PyCharm配置Anaconda環(huán)境的方法
這篇文章主要介紹了Mac中PyCharm配置Anaconda環(huán)境的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03python開發(fā)中range()函數(shù)用法實(shí)例分析
這篇文章主要介紹了python開發(fā)中range()函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python中range()函數(shù)遍歷列表的相關(guān)技巧,需要的朋友可以參考下2015-11-11Playwright中Web自動化測試的實(shí)現(xiàn)
Playwright是一個現(xiàn)代的Web自動化測試框架,本文主要介紹了Playwright中Web自動化測試的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03