Home > Blockchain >  my image is not storing on submit in django
my image is not storing on submit in django

Time:12-09

When i submit my form having image it is not saving in any folder and when i submit it from django admin panel it is saving

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="", default=None, blank=True, null=True)
dish_date = models.DateField()

def __str__(self):
    return self.dish_name

this is my setting.py

STATIC_URL = 'static/'
MEDIA_URL = 'images/'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MEDIA_ROOT = os.path.join(BASE_DIR, 'card/static/images')

this is my view.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 form

<form method="post" action="/index">
    {% 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" >Submit</button>

</form>

CodePudding user response:

I think you have to encode the data of the request this way.

<form enctype="multipart/form-data" method="POST" action="/index">

CodePudding user response:

According to your model field dish_image, you have not set folder name to upload_to.

let's set it:

dish_image = models.ImageField(upload_to="images/", default=None, blank=True, null=True) #here added images as a foldername to upload to.

And in settings.py file:

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

And in your main project urls:

Add below code in urlpatterns:

 static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

EX:

urlpatterns = [
 
] static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) #added here

Note: You don't need to create media folder in your project root. It will create media folder along with images folder in your project root

  • Related