简体   繁体   中英

enctype='multipart/form-data' is not storing images in django?

I wanted to save text and images in my database in django but when i used

enctype='multipart/form-data'

it is not storing the image.

When i do it without

enctype='multipart/form-data'

it is storing the name of image

this is my index.html `

<form method="POST" action="/index" enctype='multipart/form-data'>
        {% csrf_token %}
        <div>Dish name: <input name="dish_name" type="text" placeholder="Dish name"></div>
        <div>Dish category: <input name="dish_category" type="text" placeholder="Dish category"></div>
        <div>Dish size: <input name="dish_size" type="text" placeholder="Dish size"></div>
        <div>Dish price: <input name="dish_price" type="text" placeholder="Dish price"></div>
        <div>Dish description: <input name="dish_description" type="text" placeholder="Dish description"></div>
        <div>Dish image: <input name="dish_image" type="file"></div>
        <button type="submit" class="btn btn-success">Submit</button>

    </form>

this is my views.py

def index(request):
    if request.method == "POST":
        dish_name = request.POST.get('dish_name')
        dish_size = request.POST.get('dish_size')
        dish_price = request.POST.get('dish_price')
        dish_description = request.POST.get('dish_description')
        dish_image = request.POST.get('dish_image')
        dish_category = request.POST.get('dish_category')
        item = dish(dish_name = dish_name, dish_size = dish_size, dish_price = dish_price, dish_description = dish_description,dish_category=dish_category, dish_image = dish_image, dish_date = datetime.today()) 
        item.save()
    dishs = dish.objects.all()
    params = {'dish': dishs}
    return render(request, "card/index.html", params)

this is my models.py

class dish(models.Model):
    dish_id = models.AutoField
    dish_name = models.CharField(max_length=255, blank=True, null=True)
    dish_category = models.CharField(max_length=255, blank=True, null=True)
    dish_size = models.CharField(max_length=7, blank=True, null=True)
    dish_price = models.IntegerField(blank=True, null=True)
    dish_description = models.CharField(max_length=255, blank=True, null=True)
    # dish_image = models.ImageField(upload_to="images/", default=None, blank=True, null=True)
    dish_image = models.ImageField(upload_to="media/", default=None, blank=True, null=True) #here added images as a foldername to upload to.
    dish_date = models.DateField()

    def __str__(self):
        return self.dish_name

this is my urls.py


urlpatterns = [
    path('/index', views.index),
    path('', views.index),
    path('/', views.index),
    path('index/', views.index),
    path('index', views.index),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

this is my settings.py

MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')

`

using this my text is saving but image is not saving

In your html file, use this: enctype='multipart/form-data'

In your post method, you should use:

dish_image = request.FILES.get('dish_image')

This will return a file, which you can then save it to the model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM