我正在使用Python和Django。我试图实现一个简单的ajax调用,但函数永远不会被调用。
抱歉文件夹结构图片不想上传,但基本上
sProduct\
myapp\
sproduct\
manage.py
static\
js\
jquery-3.2.1.min.js
myapp-ajax.js
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2exl1&c-4p9g2(8!h)si1g7ilvidk&31fg%@nlkp)^32k1d28g'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
#LOGGING_CONFIG = None
#LOGGING = {"INFO"}
#import logging.config
#logging.config.dictConfig(LOGGING)
ALLOWED_HOSTS = []
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Application definition
ENVIRONMENT = "dev"
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
'markdown_deux',
'django_tables2',
'django_tables2_simplefilter',
]
index.html(基础文件)
<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery-3.2.1.min.js">
</script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/myapp-ajax.js">
</script>
MyApp的-ajax.js
$('#qty_id').change(function(){
var catid;
qid = $(this).attr("data-quoteid");
$.get('/calcTotalPrice/', {quote_id: "2"}, function(data){
$('#total').html(data);
});
});
$('#price_id').change(function(){
var catid;
qid = $(this).attr("data-quoteid");
$.get('/calcTotalPrice/', {quote_id: "2"}, function(data){
$('#total').html(data);
});
});
views.py
def calcTotalPrice(request):
print "Test"
tot = 9
return HttpResponse(tot)
日志输出
[23/May/2017 19:51:48] "GET /quote/search/quoteSearch HTTP/1.1" 200 5701
[23/May/2017 19:51:48] "GET /static/jquery-3.2.1.min.js HTTP/1.1" 404 1771
[23/May/2017 19:51:48] "GET /static/myapp/style.css HTTP/1.1" 404 1759
[23/May/2017 19:51:48] "GET /static/myapp-ajax.js HTTP/1.1" 404 1753
答案 0 :(得分:1)
你错过了路径中的js
<script type="text/javascript" src="{{ STATIC_URL }} /static/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }} /static/js/myapp-ajax.js"></script>
但您可以使用静态模板标记here
{% load static %}
<script type="text/javascript" src="{% static 'js/jquery-3.2.1.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/myapp-ajax.js' %}"></script>
答案 1 :(得分:0)
您的static/
文件夹需要位于您的应用文件夹中。因此,您似乎创建了一个名为myapp/
的应用,因此您的文件夹应如下所示:
myapp/
static/
-- static files here --
views.py
models.py
您也可以尝试在JS文件中添加console.log("test");
。如果正确加载文件,您应该在控制台中看到它。