gpt4 book ai didi

Can't change the src of un image in javascript in django(无法更改django中javascript中un-image的src)

转载 作者:bug小助手 更新时间:2023-10-22 13:50:48 29 4
gpt4 key购买 nike



So I have a javascript file that I try change the src of a image with in django. However, it keeps adding a subdirectory called the directory my image is in (the directory being in the static folder). It looks like this:

因此,我有一个javascript文件,我尝试在django中更改图像的src。然而,它不断添加一个子目录,称为映像所在的目录(静态文件夹中的目录)。它看起来是这样的:


Root/
Project/
Templates/
games/
game-one.html
static/
pre-dev/
behav.js
seven.png

in my javascript file because i cant use django tags I wrote it like this:
document.getElementById('img_eight').src = "static/pre-dev/seven.png"

在我的javascript文件中,因为我不能使用django标记,所以我这样写:document.getElementById('img_eight').src=“static/pre-dev/seven.png”


and the image adress then looks like this:
https://website.com/pre-dev/static/pre-dev/seven.png

然后图像地址看起来是这样的:https://website.com/pre-dev/static/pre-dev/seven.png


I would like to keep it as a js file, and I want to keep my images where they are

我想把它作为一个js文件,我想把我的图像放在它们所在的地方


I have tried moving my behav.js a directory down. but it gives me the same output.

我试着把我的behavio.js移到一个目录下。但它给了我同样的输出。


no matter what I change the src in my javascript file to I always get /pre-dev/.

无论我将javascript文件中的src更改为什么,我都会得到/pre-dev/。


Any help would be great.

任何帮助都会很棒。


更多回答

Seems like it automatically sets the /pre-dev/ part in the url, so just change src to seven.png. That way you should be able to access the image, since it is in the pre-dev directory.

看起来它会自动设置url中的/pre-dev/部分,所以只需将src更改为seven.png。这样你就应该能够访问映像,因为它在pre-dev目录中。

I've tried that but, the URL I need is /static/pre-dev/. and it won't let me put anything before /pre-dev/

我已经尝试过了,但是,我需要的URL是/static/pre-dev/。它不会让我在/pre-dev之前放任何东西/

So, that didn't work? Why do you need static/pre-dev/? Doesn't the static just indicate, that the files and directories within it are directly accessible throught the domain?

所以,那没用?为什么需要static/pre-dev/?静态不只是表明,它中的文件和目录可以通过域直接访问吗?

优秀答案推荐

from my experience in Javascritp when dealing with urls you have two ways of writing it

根据我在Javascritp中处理url的经验,您有两种编写方法


document.getElementById('img_eight').src = "/static/pre-dev/seven.png"

or


document.getElementById('img_eight').src = "static/pre-dev/seven.png"

notice the different here is the /

注意,这里不同的是/


the second method (the one you wrote) will create a url with the current window url
like www.example.com/current_endpoint/static/pre-dev/seven.png
but if you add a / in the beginning you will get only the domain name without the endpoint

第二种方法(您编写的方法)将创建一个具有当前窗口url的url,如www.example.com/current_endpoint/static/pe-dev/seven.png,但如果您在开头添加/,则只会获得没有端点的域名


www.example.com/static/pre-dev/seven.png

another way to improve this and make it more dynamic is to use an empty div to get data

另一种改进并使其更动态的方法是使用空div来获取数据


<div id='my_image_url' data-img-url="{{ your_context.image_url}}"></div>

hope that helps

希望能有所帮助


更多回答

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com