为什么图像没有保存在 django 的媒体目录中? [英] Why image is not getting saved in media directory in django?
问题描述
我正在尝试将图像保存在 media/images 目录中,并且已经完成了下面描述的每一个 neccosry 步骤.但毕竟,我没有在我的目录中获取图像,请有人指导我问题出在哪里.谢谢.
I am trying to save the image in media/images directory and have done each and every neccosry step which is described below. But after all, I am not getting the image in my directory please someone guide me where is the problem. Thank You.
Models.py
from django.db import models
# Create your models here.
class students(models.Model):
firstname = models.CharField(max_length=50)
lastname = models.CharField(max_length=50)
fathername = models.CharField(max_length=50)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zip = models.IntegerField(blank=True)
address = models.CharField(max_length=100)
contact =models.CharField(max_length=20)
photo = models.ImageField(upload_to='images/', height_field=None, width_field=None, max_length=None)
urls.py
from django.urls import path, include
from home import views
from django.contrib import admin
from home import StudentViews
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path("", views.index, name="index"),
path("Add_Student", views.Add_Student, name="Add_Student"),
path("display_students", views.Display_Student, name="display_students"),
path("<int:id>", views.student_profile, name="student_profile"),
#path("student/<int:id>/view", views.student_profile, name="student_profile"),
path("Student_Home", StudentViews.StudentHome, name="Student_Home")
]
#this line is for media
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
settings.py
在 settings.py 中,我添加了以下代码行.
In settings.py, I added the following lines of code.
MEDIA_ROOT= os.path.join(BASE_DIR, 'media/images')
MEDIA_URL= "/images/"
我的目录
Student_Management_System
Student_Management_System
- 家
- 媒体/图片
- 模板
- 静态
注意:我已经安装了 Pillow.
Note: I have already installed Pillow.
views.py
这是我的视图 Add_Student
将条目添加到数据库中.
Here is my view Add_Student
that add the entry into database.
from django.shortcuts import render, redirect
from home.models import students
# Create your views here.
def index(request):
return render(request, "index.html")
def Add_Student(request):
if request.method == 'POST':
firstname = request.POST.get('firstname')
lastname = request.POST['lastname']
fathername = request.POST['fathername']
city = request.POST['city']
country = request.POST['country']
state = request.POST['state']
zipcode = request.POST['zip']
address = request.POST['address']
contact = request.POST['contact']
photo = request.POST['photo']
Add_Student = students.objects.create(firstname=firstname, lastname=lastname, fathername=fathername, city=city,country=country, state=state, zip=zipcode, address=address, contact=contact, photo=photo)
Add_Student.save()
return redirect('Add_Student')
else:
return render(request, 'student/Add_Student.html')
推荐答案
更新您的 MEDIA_ROOT
MEDIA_ROOT= os.path.join(BASE_DIR, 'your_app_name/media')
您可以阅读文档 第2点
定义 upload_to
选项指定用于上传文件的 MEDIA_ROOT 子目录.
defining the
upload_to
option specifies a subdirectory of MEDIA_ROOT to be used for uploaded files.
还要确保您已将 enctype="multipart/form-data"
form 属性添加到您的模板中,如 文档
Also make sure that you have added enctype="multipart/form-data"
form attribute to your template as mentioned here in the documentation
请注意,如果请求方法是 POST 并且发布请求的方法具有属性 enctype=multipart/form-data",则 request.FILES 将仅包含数据.否则 request.FILES 将为空.
Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.
在Add_student
视图中也有一点变化
Also a little change in Add_student
view
photo = request.FILES['photo']
Add_Student.save() # NO need to use this. create() creates and saves the object at the same time.
你可以阅读这个rel="nofollow noreferrer这里
这篇关于为什么图像没有保存在 django 的媒体目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!