gpt4 book ai didi

javascript - Jquery 与 Django

转载 作者:行者123 更新时间:2023-11-28 03:11:58 25 4
gpt4 key购买 nike

在我的 Django 应用程序中未检测到 JQuery:

我有一个 html 页面 links.html :

<link rel="stylesheet" type="text/css" href="/static/css/mysheet.css">
<script type="text/javascript" src="/static/js/jquery.js"></script>
<script type="text/javascript" src="/static/js/myscript.js"></script>

我有一个 html 基础 base.html :

<!DOCTYPE html>
<html lang="us">
<head>
<meta charset="utf-8">
{% include "links.html" %}
</head>
<body>
{% load static %}
...

但是当我想在页面 X.html 中使用 js 代码时,它不起作用:

X.html :

{% extends "base.html" %}
{% block title %} <title> page X </title> {% endblock %}
{% block content %}
<a href="#" id="linkID"> Blabla </a>
{% endblock %}

我的脚本.js

$("#linkID").click(function(){
alert("ok it's good");
});

没有显示...但是当我使用简单的 javascript(没有 jquery 和 ajax)时,它起作用了!

在我的 settings.py 中,我在 INSTALLED_APPS 中有“django.contrib.staticfiles”,我有这个:

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

感谢您的帮助!

最佳答案

在 Django 中指定静态文件时,我们必须使用 {% static %} template command而不是指定文件的路径。

link.html可以修改为

{% load staticfiles %}
.....
.....

<link rel="stylesheet" type="text/css" href="{% static "css/mysheet.css" %}">
<script type="text/javascript" src="{% static "js/jquery.js" %}"></script>
<script type="text/javascript" src="{% static "js/myscript.js" %}"></script>

关于javascript - Jquery 与 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30150948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com