本文介绍了python django图像上传不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加数据并单击Submit按钮时,Python Django图像上传不起作用,没有给出任何响应,也没有上传数据库中的任何数据.

models.py首先,我添加了模型文件.

models.pyfisrst 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然后我创建一个form.py.

forms.pythen i create a 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然后我给出方法的路径.

urls.pythen 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 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

carviews.py

然后我创建了一个API方法.

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, 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

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>&nbsp;
                <a class="btn btn-primary" href="/showcar">Back</a>
              </form>
          </div>

是的,我还安装了枕头

推荐答案

按照文档,则需要在 form 元素上设置 enctype ,然后传入 request.FILES 到表单.

As per the documentation, you need to set the enctype on the form element and then pass in request.FILES to the form.

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

然后在您看来:

form = CarForm(request.POST, request.FILES)

这篇关于python django图像上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:32