Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
Get relative path in Django

I've created a map where I want to implement some kml files into it . If i harcode the url it works but im trying to pass it through a variable because Im dealing with many kml files in a for loop. Even though the url path im getting in console is right i dont get the result i need.Any idea how to fix that?

view:

def map(request):
    field_list = models.Field.objects.all()
    context = {
        "title": "Map",
        "field_list": field_list,
    }
    template = 'agriculture/map.html'
    return render(request, template, context)

If i hardcode the url it goes like this :

var polygon = omnivore.kml("{% static '../media/kml/user_admin/2022-04-07-2-Arnissa_cherry.kml' %}", ... );

I've tried doing it like this but even though the path Im getting is correct it seems django does not read the path(kml is the FileField in my model):

map.html

{% for field in field_list %}
    $(".search_area").append(new Option("{{field.friendly_name}}")); //friendly name 
   
var kmldir = "../media/" + "{{field.kml.name}}"
console.log(kmldir) // ../media/kml/user_admin/2022-04-07-2-Arnissa_cherry.kml

var polygon = omnivore.kml("{% static 'kmldir' %}", null, new L.GeoJSON(null, { //file url
    style: function() {
        return {
            color: 'red',
            transparent: true,
            opacity: 1,
            fillOpacity: 0.05
        }}     
}));  
kml_arr.push([polygon, "{% static 'kmldir' %}"]); //file url
{% endfor %}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I tried a different approach and it worked

{% for field in field_list %}

    {% with "../media/"|add:field.kml.name as fieldname %}       

        $(".search_area").append(new Option("{{ field.friendly_name }}")); //friendly name 

         var polygon = omnivore.kml('{% static fieldname %}', null, new L.GeoJSON(null, { //file url
            style: function() {
                return {
                    color: 'red',
                    transparent: true,
                    opacity: 1,
                    fillOpacity: 0.05
                }
            }
        }));

             kml_arr.push([polygon, '{% static fieldname %}']); //file url
    {% endwith %}

{% endfor %}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda