pythonファイルをローカルサーバにアップロードします.

17717 ワード

1:pythonのアップロードファイル
1.1.urlコード

 1 """untitled1222 URL Configuration
 2 
 3 The `urlpatterns` list routes URLs to views. For more information please see:
 4     https://docs.djangoproject.com/en/2.1/topics/http/urls/
 5 Examples:
 6 Function views
 7     1. Add an import:  from my_app import views
 8     2. Add a URL to urlpatterns:  path('', views.home, name='home')
 9 Class-based views
10     1. Add an import:  from other_app.views import Home
11     2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
12 Including another URLconf
13     1. Import the include() function: from django.urls import include, path
14     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
15 """
16 from django.contrib import admin
17 from django.urls import path
18 from app01 import views
19 urlpatterns = [
20     path('admin/', admin.site.urls),
21     path('upload/',views.upload),
22 ]
View Code
1.2.viewsコード

 1 from django.shortcuts import render
 2 from django.shortcuts import HttpResponse
 3 
 4 # Create your views here.
 5 def upload(request):
 6     if request.method=='GET':
 7         return render(request,'upload.html')
 8     else:
 9         user=request.POST.get('user')
10         print(user)
11         #img     ,     ,    、  ....
12         img=request.FILES.get('img')
13         print(img)
14         print(img.name)
15         print(img.size)
16 
17         #        
18         f=open(img.name,'wb')
19         for line in img.chunks():
20             f.write(line)
21         f.close()
22 
23         return HttpResponse('OK')
View Code
1.3.templatesの中でuplload.

 1 
 2 "en">
 3 
 4     "UTF-8">
 5     Title
 6 
 7 
 8 
"/uplad/"method="post"enctype="multiipad/form-data"
9「text」name=「user」
10「file」name=「img」
11「submit」value=「提出」
12
13
14
View Code
1.4.効果表示
python 文件上传本地服务器_第1张图片
 
2.ファイルアップロードボタンの最適化
2.1ボタンの最適化は既存のuploady.ファイルの中で関連様式の設定をすればよく、重点設定の透明度:opacity:0

 1 
 2 "en">
 3 
 4     "UTF-8">
 5     Title
 6 
 7 
 8 
"/uplad/"method="post"enctype="multiipad/form-data"
9「text」name=「user」
10「position:relative;」
11 アップロード
12"file"name="img"style="opacity:0.2;position:absolute;top:0;left:0">
13
14「submit」value=「提出」
15
16"</spangt;<span style=""gt;/static/jquery-33.11.min.js</spangt;<span style="カラー:唨">"/"spant>gt;
17
18
View Code
2.2.最適化後の効果表示:
python 文件上传本地服务器_第2张图片
 
3.pythonのFormコンポーネントファイルのアップロード(上記のカスタムアップロードファイルとの違い:formアップロードファイルは検証機能が多い)

 1 from django.shortcuts import render
 2 from django.shortcuts import HttpResponse
 3 
 4 
 5 # Create your views here.
 6 from django import forms
 7 from django.forms import fields
 8 #from         
 9 class UploadImg(forms.Form):
10     user=fields.CharField()
11     img=fields.FileField()
12     
13     
14 def upload(request):
15     if request.method=='GET':
16         return render(request,'upload.html')
17     else:
18         OBJ=UploadImg(request.POST,request.FILES)
19         if OBJ.is_valid():
20             user=OBJ.cleaned_data['user']
21             img=OBJ.cleaned_data['img']
22             f = open(img.name, 'wb')
23             for line in img.chunks():
24                 f.write(line)
25             f.close()
26         #         
27         # def upload(request):
28         #     if request.method == 'GET':
29         #         return render(request, 'upload.html')
30         #     else:
31         #         user=request.POST.get('user')
32         #         print(user)
33         #         #img     ,     ,    、  ....
34         #         img=request.FILES.get('img')
35         #         print(img)
36         #         print(img.name)
37         #         print(size)
38         #         f = open(img.name, 'wb')
39         #         for line in img.chunks():
40         #             f.write(line)
41         #         f.close()
42         #         return HttpResponse('OK')
43         return HttpResponse('OK')
View Code
 
転載先:https://www.cnblogs.com/506941763lcj/p/10163400.html