【Python】python-django上傳下載功能

小亮520cl發表於2017-03-15

1.models設計

  1. from django.db import models

  2. # Create your models here.
  3. class User(models.Model):
  4.     username = models.CharField(max_length = 30)
  5.     headImg = models.FileField(upload_to = './static/upload/')                ##重點,上傳路徑,專案/static/upload下

  6.     def __unicode__(self):
  7.         return self.username

2.view檢視設計

  1. #coding=utf-8
  2. from django.shortcuts import render,render_to_response
  3. from django import forms
  4. from django.http import HttpResponse
  5. from disk.models import User

  6. # Create your views here.
  7. class UserForm(forms.Form):
  8.     username = forms.CharField()
  9.     headImg = forms.FileField()

  10. def register(request):
  11.     if request.method == "POST":
  12.         uf = UserForm(request.POST,request.FILES)
  13.         if uf.is_valid():
  14.             #獲取表單資訊
  15.             username = uf.cleaned_data['username']
  16.             headImg = uf.cleaned_data['headImg']
  17.             #寫入資料庫
  18.             user = User()
  19.             user.username = username
  20.             user.headImg = headImg
  21.             user.save()
  22.             return HttpResponse('upload ok!')
  23.     else:
  24.         uf = UserForm()
  25.     return render_to_response('register.html',{'uf':uf})

  26. def listpag(request):
            listpag=User.objects.all()
            return render_to_response('listpag.html',locals())

urls配置省略。。。。

3.html設計

  1. 上傳頁面
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
  4. <html xmlns="" xml:lang="en" lang="en">
  5. <head>
  6.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7.         <title></title>
  8.         </head>
  9.         <body>
  10.         <h1>register</h1>
  11.         <form method="post" enctype="multipart/form-data" >
  12.         {{uf.as_p}}                                              ### as_p就是全部欄位的意思
  13.         <input type="submit" value="ok"/>
  14.         </form>
  15.         </body>
  16.         </html>


  17. 顯示頁面
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
  20. <html xmlns="" xml:lang="en" lang="en">
  21. <head>
  22.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  23.         <title></title>
  24.         {% load staticfiles %}
  25.         </head>
  26.         <body>
  27.         <h1>register</h1>
  28.         <img src="/static/upload/2.jpg" alt="鬱金香" />                 ###以靜態路徑的方式呼叫
  29.         <img src="{% static 'upload/2.jpg' %}" alt="鬱金香" />         ###以tag的方式呼叫圖片

  30.                   {% for listpag in listpag%}
  31.                   {{ listpag.username }}
  32.                   {{ listpag.headImg }}
  33.                   {% endfor %}
  34.                
  35.         </body>
  36.         </html>

4.setting.py設定
  1. STATIC_URL = '/static/'
  2. 上面那句是寫死的,讓django能呼叫靜態檔案,下面這句是配置靜態檔案的路徑,路徑可以填寫多個,用逗號隔開
  3. STATICFILES_DIRS =
  4.     os.path.join(BASE_DIR, "static"),

  5. BASE_DIR一般是django定義好的,在setting.py的最上面:
  6. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

  7. os.path.join的作用是把兩個路徑拼接成一個完整的路徑。
  8. 上面配置修改完,還需要在urls.py中加入配置,首先要引入模組
  9. from django.conf.urls.static import static
  10. from django.conf import settings


  11. 至此,django就可以呼叫靜態檔案了,模版檔案還要注意寫法,{% load staticfiles %}不要忘記寫,可見第三步驟

參考文件:
http://www.cnblogs.com/tramp/p/5230270.html
http://www.cnblogs.com/fnng/p/3740274.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2135397/,如需轉載,請註明出處,否則將追究法律責任。

相關文章