【问题标题】:Django: want to create an app that can upload and display imagesDjango:想创建一个可以上传和显示图像的应用程序
【发布时间】:2011-10-06 02:46:55
【问题描述】:

我想创建一个可以上传和显示图像的 django 应用程序。但是,我在执行此操作时遇到了一些麻烦,因为它无法正常工作。发生的情况是,当我尝试从表单上传图像时,它不会显示在我的列表中。所以我似乎在显示我的图片时遇到了问题。

这是我到目前为止所做的。

编辑 - 代码已更新: 我做了一些需要进行的更改。现在还是有问题。我的程序不是打印图像,而是打印保存图像的目录。

例如,{{comment.photo}} 将打印出路径C:/Users/AQUIL/Desktop/myproject/images/P1000992.JPG。但我想在屏幕上看到图像。如何将图像打印到屏幕上?

models.py

class Comment(models.Model):
    name = models.CharField(max_length = 40)
    datetime = models.DateTimeField(default=datetime.now)
    photo = models.ImageField(upload_to='C:/Users/AQUIL/Desktop/myproject/images', blank=True, null=True)
    note = models.TextField()
    def __unicode__(self):
        return unicode(self.name)

Views.py

def home(request):
    comments = None
    try:
        comments = Comment.objects.order_by('-datetime')
    except:
        return HttpResponseNotFound()
    return render_to_response('home.html', {'comments':comments}, context_instance=RequestContext(request)) 


def add_notes(request):
    comments = Comment.objects.all()
    if request.method == 'POST':
        form = CommentForm(request.POST or None, request.FILES)
        if form.is_valid():
            comments.datetime = datetime.now()
            form.save(True)
            return HttpResponseRedirect(reverse(home))
    else:
        form = CommentForm()
    return render_to_response('form.html', {'form':form,'comments':comments}, context_instance = RequestContext(request))

forms.py

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
    exclude =('datetime')

home.html

{% extends "base.html" %}

{% block content %}

<H2>List of Comments</H2>
<div style="overflow:auto;padding: 10px; border:1px solid black; height:150px; width:700px;">

{% for comment in comments %}
    {{comment.photo}}<br/>
    <b>Posted by: {{ comment.name }} Date: {{ comment.datetime.date }} Time: {{comment.datetime.time}}</b><br/>
    <div style="font-size:125%">{{ comment.note }}</div><br/>   
{% endfor %}
</div>
{% endblock %}

这是很多信息,但我希望这会有所帮助。 form.html

{% extends "base.html" %}
{% block content %}

<h3>Add Notes</h3>  
    <form  enctype="multipart/form-data"  action="" method="POST">{% csrf_token %}
        <table>
        {{form.as_table}}<br/>
        </table>
         <input type="submit" value="Save" STYLE="background-color:#E8E8E8; color:#181818 "/>
    </form>
{% endblock %}

【问题讨论】:

  • 好的,所以你必须将 enctype="multipart/form-data" 添加到你的表单标签中,否则它不会上传任何图片
  • 你必须在settings.py之前配置更多的东西; MEDIA_ROOTMEDIA_URL 之类的东西。然后您必须将图像目录放在 html img 标记中,在您的情况下,它可能看起来像:&lt;img src="/media/images/{{comment.photo}}"&gt;。请参阅stackoverflow django image question 了解更多 Django 详细信息和w3schools img tag 了解更多 HTML 详细信息。

标签: python django image forms upload


【解决方案1】:

首先,您的网址字符串应该只包含斜杠“/”符号而不是“\”

其次,您可以为 upload_to 参数使用本地路径:

upload_to='upload/'

然后在settings.py中正确设置MEDIA_ROOT值,见docs

在你的情况下,它会是:

MEDIA_ROOT = 'C:/Users/AQUIL/Desktop/myproject/images/'

【讨论】:

  • 是的,基本上你必须合并我们的两个答案才能达到你的目的
【解决方案2】:

阅读文档中的this

另外,我们可以看到表单的 html 代码吗?顺便说一句,如果您完全按照文档中的方式进行操作,您将解决它... enctype 是基本的!

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    相关资源
    最近更新 更多