How to upload image in django

how to upload images in django ?

We will be creating a model first in the file in our installed app.

from django.db import models
# Create your models here.
import datetime
import os

def filepath(requestfilename):
    old_filename = filename
    timeNow ='%Y%m%d%H:%M:%S')
    filename = "%s%s" % (timeNowold_filename)
    return os.path.join('uploads/'filename)

class Item(models.Model):
    name = models.TextField(max_length=191)
    price = models.TextField(max_length=50)
    description = models.TextField(max_length=500null=True)
    image = models.ImageField(upload_to=filepathnull=Trueblank=True)

Here, we are creating a model/table Item and we have one image field. We have written a function filepath() to generate a unique name for images whenever we are uploading any image. After creating the model "Item", just register your models in the file.

from django.contrib import admin
from .models import Item
# Register your models here.

Once you have registered your models, run the command

py makemigrations

and your migrations will be ready. Run the command

py migrate

and your table will be migrated to your database.

Now as we are ready with the database, let's write the path and write the function to insert the image into database.

create a file in the application you created inside the project and paste the below code:

from django.urls import path
from . import views

urlpatterns = [

In the file, create a function addProduct and paste the below code :

def addProduct(request):
    if request.method == "POST":
        prod = Item() = request.POST.get('name')
        prod.description = request.POST.get('description')
        prod.price = request.POST.get('price')

        if len(request.FILES) != 0:
            prod.image = request.FILES['image']
        messages.success(request"Product Added Successfully")
        return redirect('/')
    return render(request'products/add.html')

 The add.html file would look like :

{% extends 'products/layouts/main.html' %}

{% block content %}

<section class="section">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h2 class="fw-bold">Add Product</h2>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                            {% csrf_token %}
                            <div class="mb-3">
                                <label for="" class="form-label">Name</label>
                                <input type="text" Required name="name" class="form-control">
                            <div class="mb-3">
                                <label for="" class="form-label">Descritpion</label>
                                <input type="text" Required name="description" class="form-control">
                            <div class="mb-3">
                                <label for="" class="form-label">Price</label>
                                <input type="text" Required name="price" class="form-control">
                             <div class="mb-3">
                                <label for="" class="form-label">Image</label>
                                <input type="file" Required name="image" class="form-control">
                            <button type="submit" class="btn btn-primary">Save</button>
{% endblock content %}

Add the below code in the project's file 

from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT