Django中没有上传图像

kjthegm6  于 2023-01-31  发布在  Go
关注(0)|答案(1)|浏览(122)

我尝试使用两种不同的方法保存记录,但都不起作用。

  1. Django形态
    1.模型(创建方法)
    1我创建了一个ModelForm
class ProductForm(ModelForm):
    class Meta:
        model= ProductDetails
        fields= ("name","category","subcategory","price","mrp","product_details","main_img","img1","img2","img3")

        labels={
            "name":"Product Name",
            "product_details":"Description",
            "category":"Category",
            "subcategory":"Sub-Category",
            "price":"Price",
            "mrp":"MRP",
            "main_img":"Main Image",
            "img1":"Image 1",
            "img2":"Image 2",
            "img3":"Image 3",
        }

        widgets = {
            'name':forms.TextInput(attrs={'class':'form-control validate',}),
            'product_details':forms.TextInput(attrs={'class':'form-control validate',}),

            'category':forms.TextInput(attrs={'class':'custom-select tm-select-accounts',}),
            'subcategory':forms.TextInput(attrs={'class':'custom-select tm-select-accounts',}),

            'price':forms.TextInput(attrs={'class':'form-control validate',}),
            'mrp':forms.TextInput(attrs={'class':'form-control validate',}),

            'main_img':forms.FileInput(attrs={'class':'btn btn-primary btn-block mx-auto',}),
            'img1':forms.FileInput(attrs={'class':'btn btn-primary btn-block mx-auto',}),
            'img2':forms.FileInput(attrs={'class':'btn btn-primary btn-block mx-auto',}),
            'img3':forms.FileInput(attrs={'class':'btn btn-primary btn-block mx-auto',}),
            
        }

型号文件

# For Product details
class ProductDetails(models.Model):
    name= models.CharField(max_length=100)
    price= models.FloatField()
    mrp= models.FloatField()
    main_img = models.ImageField(upload_to='product_img')
    img1 = models.ImageField(upload_to='product_img')
    img2 = models.ImageField(upload_to='product_img')
    img3 = models.ImageField(upload_to='product_img')
    category = models.ForeignKey(Category, related_name='produits', on_delete=models.CASCADE)
    subcategory = models.ForeignKey(SubCategory, related_name='produits', on_delete=models.CASCADE)
    product_details = RichTextField(blank=True, null=True)
    trending = models.BooleanField(default=False)
    
    def __str__(self):
        return self.name
    • 方法1**

使用www.example.com()保存记录时出现表单验证错误,我尝试从所有位置(Forms.py、模板、视图、py)删除main_img,img1,img2,img3。然后没有验证错误,记录成功保存。form.save() getting Form validation error I have tried by removing main_img,img1,img2,img3 from all place (Forms.py, template, views,py). Then there is not validation error and record is getting saved successfully.
验证错误只是由于图像上载的某些问题
打印(表格错误)= <ul class="errorlist"><li>main_img<ul class="errorlist"><li>This field is required.</li></ul></li><li>img1<ul class="errorlist"><li>This field is required.</li></ul></li><li>img2<ul class="errorlist"><li>This field is required.</li></ul></li><li>img3<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

def add_product(request):
    if request.method == "POST":
        form = ProductForm(request.POST or None, request.FILES or None)
        print(form.errors )
        if form.is_valid():
            category_id = request.POST.get('category')
            subcategory_id = request.POST.get('subcategory')
            category= Category.objects.get(id=int(category_id))
            subcategory= SubCategory.objects.get(id=int(subcategory_id))
            form.category = category
            form.subcategory = subcategory
            form.save()
            return redirect("/dashboard/products")
    form = ProductForm()
    categories=Category.objects.all()
    subcategories=SubCategory.objects.all()
    return render(request, "01-add-product.html", "form":form,"categories":categories,"subcategories":subcategories})
    • 方法二**

尝试使用www.example.com保存记录()记录被保存,但图像没有被上传到媒体文件夹从Django-admin保存记录时,图像被上传到正确的位置,即/media. product_img/...但从这个外部HTML显示Django-admin中的文件名,但文件在媒体文件夹中不可用models.save() The record is getting saved but image is not getting uploaded to the media folder On saving record from Django-admin the image is getting uploaded to proper place i.e. /media.product_img/... But From this outside HTML its showing the file name in Django-admin but the file is not available in media folder
我已经在我的项目/www.example.com中添加了urlpattern + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)。还尝试在此应用程序的urls.py中添加相同内容urls.py. Also tried adding the same in urls.py of this app
注=我在模型中提到了upload_to=product_img(了解这一点可能很重要)

def add_product(request):
    if request.method == "POST":        
        name = request.POST['name']
        product_details = request.POST['product_details']
        category_id = request.POST.get('category')
        subcategory_id = request.POST.get('subcategory')
        category= Category.objects.get(id=int(category_id))
        subcategory= SubCategory.objects.get(id=int(subcategory_id))
        price = request.POST['price']
        mrp = request.POST['mrp']
        main_img = request.POST['main_img']
        img1 = request.POST['img1']
        img2 = request.POST['img2']
        img3 = request.POST['img3']
        product = ProductDetails.objects.create(name=name,product_details=product_details,category=category,subcategory=subcategory,price=price,mrp=mrp,main_img=main_img,img1=img1,img2=img2,img3=img3)
        product.save()

这是我的模板

{% extends '01-admin-base.html' %}
{% load static %}
{% block content %}

    <div class="container tm-mt-big tm-mb-big">
      <div class="row">
        <div class="col-xl-9 col-lg-10 col-md-12 col-sm-12 mx-auto">
          <div class="tm-bg-primary-dark tm-block tm-block-h-auto">
            <div class="row">
              <div class="col-12">
                <h2 class="tm-block-title d-inline-block">Add Product</h2>
              </div>
            </div>
            <div class="row tm-edit-product-row">
              <div class="col-xl-6 col-lg-6 col-md-12">
                <form action="" method="post" class="tm-edit-product-form">
                  {% csrf_token %}
                  {{form.media}}
                  <div class="form-group mb-3">
                    <label
                      for="name"
                      >Product Name
                    </label>
                    {{form.name}}
                    
                  </div>
                  
                  <div class="form-group mb-3">
                    <label
                      for="category"
                      >Category</label
                    >
                    <select
                      class="custom-select tm-select-accounts"
                      id="category" name="category"
                    >
                      <option selected>Select category</option>
                      {% for category in categories %}
                      <option value="{{category.id}}">{{category.name}}</option>
                      {% endfor %}
                    </select>
                  </div>
                  <div class="form-group mb-3">
                    <label
                      for="subcategory"
                      >Sub Category</label
                    >
                    <select
                      class="custom-select tm-select-accounts"
                      id="subcategory" name="subcategory"
                    >
                      <option selected>Select sub-category</option>
                      {% for subcategory in subcategories %}
                      <option value="{{subcategory.id}}">{{subcategory.name}}</option>
                      {% endfor %}
                    </select>
                  </div>
                  <div class="row">
                      <div class="form-group mb-3 col-xs-12 col-sm-6">
                          <label
                            for="price"
                            >Price
                          </label>
                          {{form.price}}
                        </div>
                        <div class="form-group mb-3 col-xs-12 col-sm-6">
                          <label
                            for="mrp"
                            >MRP
                          </label>
                          {{form.mrp}}
                        </div>
                  </div>
                  <div class="form-group mb-3">
                    <label
                      for="description"
                      >Description</label
                    >
                    {{form.product_details}}
                  </div>
                  
              </div>
              
              <div class="col-xl-6 col-lg-6 col-md-12 mx-auto mb-4">
                <div class="custom-file mt-3 mb-3">
                  <label>Main Image</label>
                  {{form.main_img}}
                </div>
                <br><br>
                <label>Images</label>
                <div class="custom-file mt-3 mb-3">
                  {{form.img1}}                  
                </div>
                <div class="custom-file mt-3 mb-3">
                  {{form.img2}}                  
                </div>
                <div class="custom-file mt-3 mb-3">
                  {{form.img3}}                  
                </div>
              </div>

              <div class="col-12">
                <button type="submit" class="btn btn-primary btn-block text-uppercase">Add Product Now</button>
              </div>
            </form>
            </div>
          </div>
        </div>
      </div>
    </div>

{% endblock content %}

请帮我找到解决这个问题的办法。

wpx232ag

wpx232ag1#

您需要以Html格式添加enctype="multipart/form-data">,以便:

<form action="/" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
      <label for="fname">First name:</label>
      <input type="text" id="fname" name="fname"><br><br>
      <label for="lname">Last name:</label>
      <input type="text" id="lname" name="lname"><br><br>
      <input type="submit" value="Submit">
 </form>

相关问题