使用静态文件填充随机图像Python/Django

使用静态文件填充随机图像Python/Django,python,django,image,templates,static,Python,Django,Image,Templates,Static,应用:价格是正确的游戏 我正在尝试将静态资源文件夹中的随机图像(我正在使用图像和css文件的资源)填充到我的一个模板中。代码正在工作,但我得到一个404图像未找到错误。我觉得问题在我的道路上 我还有一个问题,我还有一个随机数函数,我能在同一个文件(my_templatetag.py)中同时使用这两个函数吗?当两个函数都在同一个文件中时,我会出错。我的random_int数运行良好,但是当我尝试运行random_image函数时,我得到了很多错误 谢谢你的帮助 my_templatetag.py(

应用:价格是正确的游戏

我正在尝试将静态资源文件夹中的随机图像(我正在使用图像和css文件的资源)填充到我的一个模板中。代码正在工作,但我得到一个404图像未找到错误。我觉得问题在我的道路上

我还有一个问题,我还有一个随机数函数,我能在同一个文件(my_templatetag.py)中同时使用这两个函数吗?当两个函数都在同一个文件中时,我会出错。我的random_int数运行良好,但是当我尝试运行random_image函数时,我得到了很多错误

谢谢你的帮助

my_templatetag.py(随机数和图像定义)

retailPrice_start.html

{% extends 'base_layout.html'%}
    {% load my_templatetag %}
    {% load static from staticfiles %}

    {% block content %}
    <h1>Actual Retail Price!</h1>
    <img src="{% static 'random_image' %}" alt""</img>
    <p>Input where player can enter amount + button to enter guess</p>
    <p>Include other players guesses via random players and amounts</p>

    <p>python will go through guess and see if player is winner, if you win you move to last and final round, if not player can play again</p>

    <form>
      <div class="form-row d-flex justify-content-center text-center">
        <div class="col-3"><i class="fas fa-user fa-5x d-flex justify-content-center"></i>
          <input type="text" class="form-control mb-2 mr-sm-2" value="{% random_int 1 500000 %}">
          <br>

        </div>
        <div class="col-3 text-center"><i class="fas fa-user fa-5x d-flex justify-content-center"></i>
          <br>
          <input type="text" class="form-control mb-2 mr-sm-2" value="" placeholder="Enter Price Here">
          <button type="submit" class="btn btn-success mb-2">Enter</button>
        </div>
        <div class="col-3"><i class="fas fa-user fa-5x d-flex justify-content-center"></i>
          <input type="text" class="form-control mb-2 mr-sm-2" value="{% random_int 1 500000 %}">
          <br>

        </div>
      </div>
    </form>

    {% endblock %}
设置.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'assets'),
)

您可以查看生成随机img文件名的逻辑,然后使用静态标记使用图像名,如
{%static random\u img\u name%}
Hi@AvinashRaj-我只需要更多的解释。你是说要去掉我的my_templatetag.py文件,把我的def添加到我的retailPrice views.py中?你可以把生成随机img文件名的逻辑放在视图中,然后使用静态标记使用图像名,比如
{%static random_img_name%}
Hi@AvinashRaj-我需要更多的解释。你是说要去掉我的my_templatetag.py文件,把我的def添加到my retailPrice views.py中?
urlpatterns += staticfiles_urlpatterns()
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'assets'),
)