如何使用Django在单个页面上上传和显示图像?

时间:2018-09-06 15:03:13

标签: django-templates

 model.py
  1. class Image(models.Model):             名称= models.CharField(max_length = 30)             图片=模型。ImageField(upload_to ='照片')

    view.py     def image_create(request,template_name ='photo / form.html'):         形式= ImageForm(request.POST或无,request.FILES或无)         如果form.is_valid():             form.save()

    return render(request, template_name, {'form':form})    
    

upload.html

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
<img src="{{ image.image.url }}"  alt="ko" width="500px" height="500 px" />

enter image description here

0 个答案:

没有答案