当前位置:   article > 正文

Django [实战] 通过表单上传文件

Django [实战] 通过表单上传文件

在Django中上传文件通常涉及到以下几个步骤:

  1. 设置你的模型以包含一个FileFieldImageField(如果上传的是图片)。
  2. 创建一个表单,该表单包含一个FormModelForm,其中包含一个FileField
  3. 在你的视图中处理上传的文件。
  4. 在你的模板中创建一个HTML表单,该表单包含一个文件输入字段。

1,创建应用

Test/app14

python manage.py startapp app14

2,注册应用

Test/Test/settings.py

3,添加应用路由

Test/Test/urls.py

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. urlpatterns = [
  4. path('app14/', include('app14.urls')),
  5. ]

4,添加模型

在模型中添加一个FileField

Test/app14/models.py

  1. from django.db import models
  2. # Create your models here.
  3. class Document(models.Model):
  4. document = models.FileField(upload_to='documents/')

5,添加表单

创建一个表单,用于上传文件

Test/app14/forms.py

  1. from django import forms
  2. class DocumentForm(forms.Form):
  3. document = forms.FileField()

 

 6,添加视图函数

Test/app14/views.py

  1. # 文件上传
  2. from django.shortcuts import render, redirect
  3. from .forms import DocumentForm
  4. from .models import Document
  5. def upload_document(request):
  6. if request.method == 'POST':
  7. form = DocumentForm(request.POST, request.FILES)
  8. if form.is_valid():
  9. document = Document(document=request.FILES['document'])
  10. document.save()
  11. return redirect('success_url') # 重定向到成功页面
  12. else:
  13. form = DocumentForm()
  14. return render(request, '14/upload.html', {'form': form})
  15. def success_view(request):
  16. # This view can display a success message or redirect elsewhere
  17. return render(request, '14/success.html')

 

7,执行迁移

  1. python manage.py makemigrations app14
  2. python manage.py migrate app14

8,添加HTML代码

注意,文件上传的表单需要设置enctype="multipart/form-data"属性

Test/templates/14/Upload.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <form method="post" enctype="multipart/form-data">
  9. {% csrf_token %}
  10. {{ form.as_p }}
  11. <button type="submit">Upload</button>
  12. </form>
  13. </body>
  14. </html>

上传文件成功后,重定向至一个提示文件上传成功的页面

Test/templates/14/success.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Success</title>
  5. </head>
  6. <body>
  7. <h1>File Uploaded Successfully!</h1>
  8. </body>
  9. </html>

9,settings设置MEDIA_ROOTMEDIA_URL

Test/Test/settings.py

  1. # settings.py
  2. MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  3. MEDIA_URL = '/media/'

 

10,添加路由地址

Test/app14/urls.py

  1. from django.urls import path
  2. from . import views
  3. from django.conf import settings
  4. from django.conf.urls.static import static
  5. urlpatterns = [
  6. path('my_form_view/', views.my_form_view, name='my_form_view'),
  7. path('upload_document/', views.upload_document, name='upload_document'),
  8. path('success/', views.success_view, name='success_url'),
  9. ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

 

11,访问页面

http://127.0.0.1:8000/app14/upload_document/

 Test/media/documents/赵照_-_灯塔守望人_RZHQDK0.mp3

 可以看到我们上传的文件放在了,我们设置的目录下了

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号