【问题标题】:How do i push uploaded image file in my template input for update in django 2.0如何在模板输入中推送上传的图像文件以在 django 2.0 中更新
【发布时间】:2019-01-19 12:17:23
【问题描述】:

我想在更新模式下打开 html 模板中的特定记录,我希望之前为此记录插入的每个值都在那些字段中,我在文本字段的字段中获得了每个必需的值,但在包含的文件字段中图像,它显示没有选择文件,因为我想要图像文件名(url)在那里。

我没有使用 django 表单,而是使用了普通的引导程序表单。

models.py

from django.db import models
from django.contrib.auth.models import User

class Product(models.Model):
    title = models.CharField(max_length=255)
    pub_date = models.DateTimeField()
    body = models.TextField()
    image = models.ImageField(upload_to='images/')   # i m facing problem for this field
    icon = models.ImageField(upload_to='images/')   # i m facing problem for this field as well 
    url = models.TextField()
    votes_total = models.IntegerField(default=1)
    hunter = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.title
    def summary(self):
        return self.body[:100]
    def short_pub_date(self):
        return self.pub_date.strftime('%b %e %Y')
#

views.py

def myproducts_update(request,product_id):
    product = get_object_or_404(Product,pk=product_id)
    print(product.image)          # this prints the name of the file (images/37003.jpeg)
    return render(request,'products/myproducts_update.html',{'product':product})

模板(myproducts_update.html)

{% extends 'base.html' %}

{% block content %}
{% if error %}
{{error}}

{% endif %}
<br>
<br>
<div class="container">
  <div class="jumbotron">

    <h2>Update Product</h2>
  <form action="{% url 'create' %}" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    <div class="form-group">
      <label for="title">Title:</label>
      <input type="text" class="form-control" id="email" value={{product.title}} placeholder="please enter title" name="title" required>
    </div>
    <div class="form-group">
      <label for="body">Description:</label>
      <input type="text" class="form-control" id="body" value={{product.body}} placeholder="Description" name="body" required>
    </div>
    <div class="form-group">
      <label for="url">URL:</label>
      <input type="text" class="form-control" id="url" value={{product.url}} placeholder="please enter url" name="url" required>
    </div>
    <br>
    <div class="row">
      <div class="col-sm-6">
        <div class="form-group">
          <label for="icon">
            <strong>Icon:</strong></label>
          <input type="file"  id="icon"  name="icon" value={{product.icon}} required>
        </div>
      </div>
      <div class="col-sm-6">
        <div class="form-group">
          <label for="url">
            <strong>Image:</strong></label>
          <input type="file"  id="image" placeholder="please enter url" name="image" value={{product.image}} required>
        </div>
      </div>
    </div>

<br>


    <input type="submit" value="Update Product" class="btn btn-primary">

  </form>





  </div>
</div>

{% endblock %}

Link which contains image of form where i have problem

我无法在模板中获取图片的 url,请大家帮帮我

提前致谢!

【问题讨论】:

    标签: django python-3.x django-2.0


    【解决方案1】:

    在您的 &lt;form&gt; 中尝试这样的操作:

    <div class="margintop50px">
        <img {% if object.image %} src="{{ object.image.url }}" {% else %} alt="You have no image." {% endif %}>
        <input type="file" name="image" accept="image/*">
    </div>
    

    更新: 我刚刚检查了你的views.py。很抱歉没有早点注意到,但是 myproducts_update() 函数从来没有 .save() 的任何东西,因此不会显示任何内容。尝试将该函数更改为如下所示。它假设该产品已经在其他地方创建,因为我不知道您为实现这一目标所做的任何其他视图(也许您最初从管理面板添加了所有图片/文本?就个人而言,我会更改您的函数的名称,因为您的表单转到'create',但是您显示的函数表明这是关于更新它,并且在设计它时通常不是一个明确的路径,因为createing 与'update'ing 它。因此,如果以下仍然不起作用,我需要查看您的 'create' 视图以及所有这些功能的 urlpatterns。

    无论哪种方式,都有更好的方法来改进以下(例如我上面谈到的),但这是我在不知道这些东西的情况下能给你的全部:

    views.py

    def myproducts_update(request,product_id):
        product = get_object_or_404(Product,pk=product_id)
        if request.method == 'POST':
            product.body = request.POST.get('body', False)
            product.title = request.POST.get('title', False)
            product.url = request.POST.get('url', False)
            product.icon = request.FILES.get('icon', False)
            product.image = request.FILES.get('image', False)
            product.save()
        return render(request,'products/myproducts_update.html',{'product':product})
    

    然后使用上面的示例模板让它显示在该页面上。

    【讨论】:

    • 刚刚编辑过。我是堆栈溢出的新手,所以我不确定我的原始帖子上的编辑是否会通知您更改,但我知道 cmets 这样做,所以我在这里也这么说。检查我的 UPDATE 位置下方的所有内容。
    猜你喜欢
    • 2021-06-26
    • 2015-04-21
    • 2014-01-29
    • 2019-08-28
    • 1970-01-01
    • 2010-10-28
    • 2010-12-08
    • 1970-01-01
    相关资源
    最近更新 更多