简体   繁体   English

django 设置图片src路径

[英]django set image src path

I want to set an image as a submit button form:我想将图像设置为提交按钮表单:

<form action="/final" method="post">{% csrf_token %}
    <input type="hidden" name="title" value="niloofar">
    <input type="image" name="submit" src="cat.jpg">
</form>

The image is in the template directory and the image is not shown.图片在模板目录下,图片不显示。 How should I manage it with STATIC_URL = '/static/' ?我应该如何使用STATIC_URL = '/static/'管理它?

Should I make a directory called static and put the image there?我应该创建一个名为static的目录并将图像放在那里吗?

And how the form should be changed?以及应该如何更改表格?

You need to properly configure your static files. 您需要正确配置静态文件。 More info can be found in Django docs 更多信息可以在Django文档中找到

Additionally, make sure you are writing the correct path the image, as shown in the docs: 此外,请确保您在编写正确的图像路径,如docs中所示:

{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>

Now some changes are there to be noted.现在需要注意一些变化。 You should not load your static files with你不应该加载你的 static 文件

{% load staticfiles %}

Instead you should write相反,你应该写

{% load static%}

And in source (denoted as src), you should give path of image file from static folder located in your directory.在源代码(表示为 src)中,您应该提供位于您的目录中的 static 文件夹中的图像文件的路径。 Not from the local resources.不是来自本地资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM