django - 无法显示从 mySQL 数据库上传的图像
djano - can not display an uploaded image from mySQL database
我已按应有的方式配置了所有内容,但无法显示数据库中的图像。
settings.py :
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
models.py
class Etudiants(models.Model):
numeroetudiant = models.BigIntegerField(db_column='numeroEtudiant', blank=True, null=True) # Field name made lowercase.
nom = models.CharField(max_length=255, blank=True, null=True)
prenom = models.CharField(max_length=255, blank=True, null=True)
groupe = models.BigIntegerField(blank=True, null=True)
photo = models.ImageField(blank=False, null=False, upload_to='images/')
email = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'etudiants'
def __str__(self):
return self.nom + " " + self.prenom
views.py :
def ajoutetudiant(request):
submitted = False
if request.method == "POST":
form = EtudiantsForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect("../ajoutetudiant/")
else:
form = EtudiantsForm()
if 'submitted' in request.GET:
submitted = True
return render(request, 'ajoutetudiant.html', {'form': form})
urls.py(我的项目):
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('notes.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
.html :
<img src="{{etudiant.photo.url}}"/>
图片保存在我的数据库中,如您所见。但无法显示它。
我在 mySQL 数据库中的字段是“blob”。如果它不应该是 blob 我应该放什么?
您 photo
的 MySQL 字段应该是:
`photo` varchar(100) NOT NULL,
我已按应有的方式配置了所有内容,但无法显示数据库中的图像。
settings.py :
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
models.py
class Etudiants(models.Model):
numeroetudiant = models.BigIntegerField(db_column='numeroEtudiant', blank=True, null=True) # Field name made lowercase.
nom = models.CharField(max_length=255, blank=True, null=True)
prenom = models.CharField(max_length=255, blank=True, null=True)
groupe = models.BigIntegerField(blank=True, null=True)
photo = models.ImageField(blank=False, null=False, upload_to='images/')
email = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'etudiants'
def __str__(self):
return self.nom + " " + self.prenom
views.py :
def ajoutetudiant(request):
submitted = False
if request.method == "POST":
form = EtudiantsForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect("../ajoutetudiant/")
else:
form = EtudiantsForm()
if 'submitted' in request.GET:
submitted = True
return render(request, 'ajoutetudiant.html', {'form': form})
urls.py(我的项目):
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('notes.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
.html :
<img src="{{etudiant.photo.url}}"/>
图片保存在我的数据库中,如您所见。但无法显示它。
我在 mySQL 数据库中的字段是“blob”。如果它不应该是 blob 我应该放什么?
您 photo
的 MySQL 字段应该是:
`photo` varchar(100) NOT NULL,