无法使用filefield在django中上传图像 [英] Cannot upload image in django using filefield

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

问题描述

我无法上传图片。



这是我的 models.py



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ = $ $ $ $ $ $ $ $ $ $ )
text = models.TextField()
image = models.FileField(upload_to ='post / static / images /',null = True,blank = True)
created_date = models.DateTimeField (default = timezone.now)
published_date = models.DateTimeField(blank = True,null = True)

def publish(self):
self.published_date = timezone.now ()
self.save()

def __str __(self):
return self.title

图像未上传到 'post / static / images /'



这是上传图片的模板

  {%if post.media%} 
< img src ={{post.image.url}}class =img-responsive/>
{%endif%}


解决方案

您的代码中的许多问题:


  1. 在您的模型中 - 使用字段for image uplaoding ImageField (要使用 ImageField ,您需要安装枕头):

      image = models.ImageField(upload_to ='images /',null = True ,blank = True)

    (您将将图像上传到MEDIA_ROOT文件夹的子文件夹中,名为图像


  2. 还要将图像放在媒体文件夹中 - 您必须创建一个,在 settings.py 文件夹添加:

      MEDIA_URL ='/ media /'
    MEDIA_ROOT = os。 path.join(BASE_DIR,'media /')


  3. 让django提供媒体只用于开发!)你必须添加你的主(从你的项目目录不是从您的应用程序文件夹) urls.py 文件:

     从django.conf导入设置
    从django.conf.urls.static import static

    如果settings.DEBUG:
    urlpatterns + = static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)


  4. 上传图片的简单表单示例:

     < form action ={%url upload_pic%}method =postenctype =multipart / form-data> {%csrf_token%} 
    < input id =id_imagetype =fileclass =name =image>
    < input type =submitvalue =提交/>
    < / form>

    其中 url_upload_pic 是(功能)视图应该处理图像的上传。



I am not able to upload image.

here is my models.py

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length=200)
    text = models.TextField()
    image = models.FileField(upload_to = 'post/static/images/' , null= True, blank= True)
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

the image is not getting uploaded to 'post/static/images/'

here is the template for uploading the image

    {% if post.media %}
    <img src="{{ post.image.url }}" class="img-responsive" />
    {% endif %}

解决方案

There are many problems with your code:

  1. In your model - use the field for image uplaoding ImageField (to use ImageField you need to have installed Pillow):

    image = models.ImageField(upload_to = 'images/' , null= True, blank= True)
    

    (you will upload the images in a subfolder of the MEDIA_ROOT folder named images)

  2. Also put the images in the media folder - you have to create one, in the settings.py folder add:

    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
    

  3. To let django serve your media (only for development!) you have to add in your main (from your project directory not from your app folder) urls.py file:

    from django.conf import settings
    from django.conf.urls.static import static
    
    if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

  4. An example of a simple form to upload an image:

    <form action="{% url upload_pic %}" method="post" enctype="multipart/form-data">{% csrf_token %}
        <input id="id_image" type="file" class="" name="image">
        <input type="submit" value="Submit" />
    </form>
    

    where url_upload_pic is the (function) view which should handle the upload of the image.

这篇关于无法使用filefield在django中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆