Django上傳並顯示圖片,

來源:互聯網
上載者:User

Django上傳並顯示圖片,
Django上傳並顯示圖片

非常詳細的教程,教大家一步步用Django上傳與顯示圖片。用例子學習是一個不錯的方法,下面我用一個非常簡單的例子為大家講解Django中圖片的上傳與顯示。

1. 建立名稱為‘a’的項目
1
$django-admin startproject a
2.在項目‘a’中建立名為‘b’的app
1
2
$cd a
$python manage.py startapp b
3.把b加入到settings.py中的INSTALLED_APPS中
1
2
3
4
5
6
7
8
9
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'b',
)
4.在檔案夾b下,編輯models.py,建立IMG類
1
2
3
4
5
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class IMG(models.Model):
img = models.ImageField(upload_to='upload')
5.更新資料庫
1
2
3
4
5
6
Django 1.7及以上的版本需要用以下命令
python manage.py makemigrations
python manage.py migrate

Django 1.7以下用以下命令
python manage.py syncdb
6.在檔案夾b下,編輯views.py,建立圖片上傳與顯示函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from django.shortcuts import render
from b.models import IMG
# Create your views here.
def uploadImg(request):
if request.method == 'POST':
new_img = IMG(
img=request.FILES.get('img')
)
new_img.save()
return render(request, 'b/uploadimg.html')

def showImg(request):
imgs = IMG.objects.all()
content = {
'imgs':imgs,
}
return render(request, 'b/showimg.html', content)
7.在a檔案夾下,編輯urls.py檔案
1
2
3
4
5
6
7
8
9
10
11
12
from django.conf.urls import url
from django.contrib import admin
from b import views
from django.conf.urls.static import static
from django.conf import settings


urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^upload', 'b.views.uploadImg'),
url(r'^show', 'b.views.showImg'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
8.編輯a檔案夾下的setting.py檔案,添加如下代碼:
1
2
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')
9.在b檔案夾下建立templates檔案夾,再在templates檔案夾下建立b檔案夾,再在新建立的b檔案夾下建立uploadimg.html檔案,內容如下:
1
2
3
4
5
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="img">
<button type="submit">上傳</button>
</form>
10.在uploadimg.html同目錄下建立showimg.html檔案
1
2
3
{% for img in imgs %}
<img src='{{ img.img.url }}' />
{% endfor %}
11.運行django程式
1
$python manage.py runserver
12.上傳圖片

開啟瀏覽器,輸入地址:http://127.0.0.1:8000/upload,進入圖片上傳頁面,點擊“瀏覽”,選擇要上傳的圖片,“上傳”之。因為頁面設計的比較簡單,所以大家上傳圖片後,在本頁面看不到任何變化,但確實已經上傳了;

13.顯示上傳的圖片

在瀏覽器中輸入:http://127.0.0.1:8000/show,就會看到我們已經上傳的圖片。

PS:以上步驟僅僅是非常簡單的圖片上傳與顯示,更多複雜的圖片上傳顯示問題,大家可以在次基礎上修改。

原創文章如轉載,請註明本文連結:http://www.cognize.me/2016/05/09/djangopic

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.