简体   繁体   中英

Can't link leaflet js file to my html in django

I am new to leaflet and have just created my first mapping app using Django. It is an awesome program but I just wanted to change the style of my map. I am drawing from this amazing resource however I am having trouble linking the js file with the HTML. I have never connected JS to HTML particularly within Django (not sure if it makes a difference). But I am returning 404s 24/Aug/2020 12:19:16] "GET /static/js/leaflet-providers.js HTTP/1.1" 404 1695

I have stored the js file in a folder called static, based on some good practise I read about (please disabuse me of this if it is not good practise!)

My relevant chunk of code is as follows:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Anybody</title>
        <!---leaflet css --->
        <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
           integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
           crossorigin=""/>

        <!---leaflet JS -->
        <!-- Make sure you put this AFTER Leaflet's CSS -->
        <script src="http://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
        <script src="static/js/leaflet-providers.js"/> </script>

Looks like you need to connect your static files. Add this to your settings (if you have not already):

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_URL = '/static/'

And then add {% load static %} to the top of your HTML file. You can then get your static files with:

<script src="{% static 'js/leaflet-providers.js' %}"> </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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