Django 實(shí)現(xiàn)圖片上傳和顯示過程詳解
第1章 新建工程和創(chuàng)建app
新建工程和創(chuàng)建app就不用貼出來了,我這里是測試圖片上傳的功能能否實(shí)現(xiàn),所以項(xiàng)目都是新的,正常在以有的app下就可以
第2章 模型層:
2.1創(chuàng)建數(shù)據(jù)庫
from django.dbimport models
# Create your models here.
class User(models.Model):
name= models.CharField(max_length=50)
# upload_to 指定上傳文件位置
# 這里指定存放在img/ 目錄下
headimg = models.FileField(upload_to="img/")
# 返回名稱
def__str__(self):
returnself.name
2.2初始化數(shù)據(jù)庫:
(mypy3) ➜ BBS python manage.py makemigrations Migrations for 'app01': app01/migrations/0001_initial.py - Create model User (mypy3) ➜ BBS python manage.py migrate Operations to perform: Apply all migrations: admin, app01, auth, contenttypes, sessions
第3章 修改配置文件
3.1settings中增加如下配置:
MEDIA_ROOT= os.path.join(BASE_DIR, 'media').replace("\\", "/")
MEDIA_URL = '/media/'
3.2工程的urls文件:
from django.conf.urlsimport url
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^regsiter/', views.regsiter),
# url(r'', TemplateView.as_view(template_name="app01/index.html")),
path('app01/', include('app01.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
3.3app:
from django.urlsimport path
from . import views
app_name = 'app01'
urlpatterns = [
path('add/', views.add, name='add'),
# path('index/', views.index, name='index'),
]
3.4修改模版配置:
TEMPLATES= [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
第4章 數(shù)據(jù)校驗(yàn)?zāi)K:
數(shù)據(jù)需要校驗(yàn)的情況下,如果你不想校驗(yàn),這個(gè)可以忽略
4.1在app下創(chuàng)建forms文件:
from django import forms # 表單類用以生成表單 class AddForm(forms.Form): name = forms.CharField() headimg = forms.FileField()
第5章 視圖層:
5.1編寫圖片處理邏輯
from django.shortcutsimport render
from .models import User
from .forms import AddForm
# Create your views here.
def add(request):
# 判斷是否為post 方法提交
ifrequest.method == "POST":
af = AddForm(request.POST, request.FILES)
# 判斷表單值是否和法
ifaf.is_valid():
name = af.cleaned_data['name']
headimg = af.cleaned_data['headimg']
user = User(name=name, headimg=headimg)
user.save()
returnrender(request, 'app01/index.html', context={"user":user})
else:
af = AddForm()
returnrender(request, 'app01/add.html', context={"af":af})
第6章 模版層:
上傳的html
<!-- templates/users/add.html -->
<!doctype html>
<html>
<head>
<title>Add</title>
<meta charset="utf-8">
</head>
<body>
<h1>Add!</h1>
<form method="post" enctype="multipart/form-data" action="{% url'app01:add' %}">
{%csrf_token %}
{{ af.as_p }}
<inputtype="submit" value="OK"/>
</form>
</body>
</html>
查看的html
<!-- templates/users/index.html -->
<!doctype html>
<html>
<head>
<title>Detail</title>
<meta charset="utf-8">
</head>
<body>
<p>{{user.name}}</p>
<img width="50%" height="50%"src="/media/{{ user.headimg }}">
</body>
</html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Django 實(shí)現(xiàn)圖片上傳和下載功能
- Django實(shí)現(xiàn)圖片上傳功能步驟解析
- 在django中圖片上傳的格式校驗(yàn)及大小方法
- django mysql數(shù)據(jù)庫及圖片上傳接口詳解
- Django框架文件上傳與自定義圖片上傳路徑、上傳文件名操作分析
- django將圖片上傳數(shù)據(jù)庫后在前端顯式的方法
- Django后臺(tái)獲取前端post上傳的文件方法
- 利用django如何解析用戶上傳的excel文件
- Python+django實(shí)現(xiàn)文件上傳
- django實(shí)現(xiàn)圖片上傳數(shù)據(jù)庫并顯示
相關(guān)文章
基于Python實(shí)現(xiàn)PDF轉(zhuǎn)換文件格式
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)PDF轉(zhuǎn)換文件格式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
Python os.mkdir()與os.makedirs()的使用區(qū)別
這篇文章主要介紹了Python os.mkdir()與os.makedirs()的使用區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python 正則表達(dá)式(?=...)和(?<=...)符號(hào)的使用
本文主要介紹Python 正則表達(dá)式(?=...)和(?<=...)符號(hào)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

