Python Django загрузка изображений не работает, когда я добавляю данные и нажимаю кнопку «Отправить», не дает никакого ответа, а также не загружает никакие данные в базу данных.
models.py В первую очередь я добавляю файл модели.
# 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, затем я создаю form.py.
from django import forms
from carapp.models import Car
class CarForm(forms.ModelForm):
class Meta:
model=Car
fields="__all__"
и добавьте эти две строки в settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py, затем я даю путь к методу.
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 user
path('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
, затем я создал метод API .
from django.shortcuts import render,redirect
from carapp.forms import CarForm
from carapp.models import Car
from django.core.paginator import Paginator, EmptyPage, InvalidPage
def savecar(request):
if request.method == "POST":
form= CarForm(request.POST)
if form.is_valid():
try:
form.save()
return redirect("/showcar")
except:
pass
else:
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=1
try:
cars= paginator.page(page)
except(EmptyPage,InvalidPage):
cars= paginator.page(paginator.num_pages)
return render(request,"showcar.html",{'cars':cars})
это 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>
и да, я также устанавливаю подушку