Flask Application - How to link a javascript file to website

前端 未结 4 799
情深已故
情深已故 2020-12-15 05:35

I am having some trouble getting started with using javascript files on my website (A Flask application). I start the website by running run.py which looks like this:

<
相关标签:
4条回答
  • 2020-12-15 06:07

    Try using src="app/javascript/jquery.js".

    Also, I noticed you include jquery.js twice - on line 7 and 28.

    0 讨论(0)
  • 2020-12-15 06:17

    So your index.html is at app/templates/index.html, and your jQuery is at app/javascript/jQuery.js right?

    I think your path is wrong. Try this:

    <script src="../javascript/jquery.js"></script>
    
    0 讨论(0)
  • 2020-12-15 06:29

    Ah yes, luckily I am currently developing a flask application at the moment.

    You are currently missing the static folder which by default flask looks into, a folder structure something like this:

    |FlaskApp
    ----|FlaskApp
    --------|templates
            - html files are here
    --------|static
            - css and javascript files are here
    

    Two important default folders that Flask will look into templates and static. Once you got that sorted you use this to link up with your javascript files from your html page:

    <script src="{{url_for('static', filename='somejavascriptfile.js')}}"></script>
    

    Hope that helps any questions just ask.

    Plus - A good article to read but not super related but it talks about the folder structure of flask is this: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps

    0 讨论(0)
  • 2020-12-15 06:31

    Here is what helped me eliminate the jquery part of my Flask project

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
    

    This was taken straight from Basic Template found in http://getbootstrap.com/getting-started/

    0 讨论(0)
提交回复
热议问题