Приложение: цена подходящая. Игра
Я пытаюсь вставить случайное изображение из папки статических ресурсов (я использую ресурсы для изображений и CSS-файлов) в один из моих шаблонов.Код работает, но я получаю 404 изображение не найдена ошибка.У меня есть ощущение, что проблема лежит на моем пути.
Другой вопрос, который у меня есть, у меня также есть функция случайных чисел, могу ли я иметь обе функции в одном файле (my_templatetag.py)?Я получаю ошибки, когда у меня есть обе функции в одном файле.У меня есть номер random_int, работающий нормально, однако, когда я пытаюсь работать с функцией random_image, я получаю много ошибок.
Спасибо за помощь
my_templatetag.py (случайные числа и значения изображений)
import random, os
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def random_int(a, b=None):
if b is None:
a, b = 0, a
return random.randint(a, b)
#Generate random img function
register = template.Library()
@register.simple_tag
def random_image():
path = r"/assets/img/"
random_image = random.choice([
x for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
return random_image()
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 %}
urls.py
urlpatterns += staticfiles_urlpatterns()
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)