Python Django image upload is not working when I am adding data and click on submit button not give a any response and also not upload any data in the database.
models.py fisrst of all i add model file.
# Create your models here.
from django.db import models
class Car(models.Model):carname=models.CharField(max_length=50)carmodel = models.CharField(max_length=50)carimage = models.ImageField(upload_to='images/')price=models.CharField(max_length=15)average=models.CharField(max_length=20)gear=models.CharField(max_length=200)passengers = models.CharField(max_length=200)type=models.CharField(max_length=200)Insurance=models.CharField(max_length=200)class Meta:db_table="car"
forms.py then i create a form.py.
from django import forms
from carapp.models import Carclass CarForm(forms.ModelForm):class Meta:model=Carfields="__all__"
also add this two line in settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py then i give a path of method.
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from carapp import carviews
urlpatterns = [path('admin/', admin.site.urls),#this is for admin userpath('savecar',carviews.savecar),path('showcar', carviews.showcar)
]
if settings.DEBUG:urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
carviews.py
then I created an API method.
from django.shortcuts import render,redirect
from carapp.forms import CarForm
from carapp.models import Car
from django.core.paginator import Paginator, EmptyPage, InvalidPagedef savecar(request):if request.method == "POST":form= CarForm(request.POST)if form.is_valid():try:form.save()return redirect("/showcar")except:passelse:form = CarForm()return render(request,"savecar.html",{'form':form})def showcar(request):car_list = Car.objects.all().order_by('-id')paginator = Paginator(car_list, 5)try:page=int(request.GET.get('page','1'))except:page=1try:cars= paginator.page(page)except(EmptyPage,InvalidPage):cars= paginator.page(paginator.num_pages)return render(request,"showcar.html",{'cars':cars})
this is savecar.html
<div class="card-body"><form method="POST" action="/savecar" >{% csrf_token %}<div class="form-group"><label for="carname">Car Name</label><div class="col-sm-4">{{ form.carname }}</div></div><div class="form-group"><label for="carmodel">carmodel</label><div class="col-sm-4">{{ form.carmodel }}</div></div><div class="form-group"><label for="carimage">carimage</label><div class="col-sm-4">{{ form.carimage }}</div></div><div class="form-group"><label for="price">price</label><div class="col-sm-4">{{ form.price }}</div></div><div class="form-group"><label for="average">average</label><div class="col-sm-4">{{ form.average }}</div></div><div class="form-group"><label for="gear">gear</label><div class="col-sm-4">{{ form.gear }}</div></div><div class="form-group"><label for="passengers">passengers</label><div class="col-sm-4">{{ form.passengers }}</div></div><div class="form-group"><label for="type">type</label><div class="col-sm-4">{{ form.type }}</div></div><div class="form-group"><label for="Insurance">Insurance</label><div class="col-sm-4">{{ form.Insurance }}</div></div><button type="submit" class="btn btn-primary">Submit</button> <a class="btn btn-primary" href="/showcar">Back</a></form> </div>
and yes i also install pillow