与Django精选表格抗争
我是一名新的开发人员,我遇到了一个名为selected.js的javascript库,它对我的高中项目非常有用。我的问题是,我在开始使用django Selected时遇到了一些问题,无法使用django Selected显示所选表单。我相信这是因为脱毛工具中出现了无效的断线,我还遗漏了其他一些东西 在选择pip install django之后,我已将所选内容添加到settings.py中的已安装文件中,如下所示:与Django精选表格抗争,django,jquery-chosen,Django,Jquery Chosen,我是一名新的开发人员,我遇到了一个名为selected.js的javascript库,它对我的高中项目非常有用。我的问题是,我在开始使用django Selected时遇到了一些问题,无法使用django Selected显示所选表单。我相信这是因为脱毛工具中出现了无效的断线,我还遗漏了其他一些东西 在选择pip install django之后,我已将所选内容添加到settings.py中的已安装文件中,如下所示: INSTALLED_APPS = [ 'django_python3_
INSTALLED_APPS = [
'django_python3_ldap',
'chosen',
'django_extensions',
'django_filters',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
]
然后我创建了我选择的表单:
from django import forms
from .models import User, FacilityDimension
from django.forms import ModelForm
from widgets import ChosenSelectMultiple
from chosen import forms as chosenforms
class ChosenModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ChosenModelForm, self).__init__(*args, **kwargs)
for field in self.fields:
if self.fields[field].__class__.__name__ in ['ChoiceField', 'TypedChoiceField', 'MultipleChoiceField']:
choices = self.fields[field].choices
self.fields[field] = chosenforms.ChosenChoiceField(choices=choices)
elif self.fields[field].__class__.__name__ in ['ModelChoiceField', 'ModelMultipleChoiceField']:
queryset = self.fields[field].queryset
self.fields[field] = chosenforms.ChosenModelChoiceField(queryset=queryset)
class FormFacilityDimension(ChosenModelForm):
class Meta:
model = FacilityDimension
创建表单后,我在模板中使用了以下内容,没有收到任何错误,但无法使其显示:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.js" type="text/javascript"></script>
<script src="chosen.jquery.js" type="text/javascript"></script>
{% block extra_js %}
{{ block.super }}
{{ form.media }}
{% endblock %}
I added the following javascript file, however my linting tool is showing invalid line break the first line which i can't seem to get rid of this may have something to do with my problem:
$(document).ready(function () {
$('#id_coid_name').chosen();
}
)
;
出于某种原因,当文件位于security/accounts/js/中时,它试图在/account/profile中找到它。如何更改此设置?您的js脚本应与网站css和图像的其他静态文件一起保存在您的静态文件夹中。这一行:
<script src="chosen.jquery.js" type="text/javascript"></script>
导致相对于当前页面的路径加载脚本。你应该换成
<script src= "{% static ‘security/accounts/js/chosen.jquery.js’ %}" type="text/javascript"></script>
或者相对于静态URL保存脚本的路径 如何找到我的静态根?现在我将文件移动到Security/Accounts/Static/Accounts中。我的静态URL定义为/STATIC/,这是您正在引用的根目录吗?更改后出现以下错误:django.template.exceptions.TemplateSyntaxError:第60行的块标记无效:“static”,应为“endblock”。您是否忘记注册或加载此标签?[2018年1月4日15:16:18]GET/account/profile/HTTP/1.1500 198992第60行的定义是:您需要阅读静态文件,请查看Django文档。靠近模板顶部的{%load static%}将解决此错误暂时忘记static_ROOT,这适用于已部署的应用程序。STATIC_URL告诉Django如何创建URL,在您的情况下,相对于runserver的项目根目录,它将变成/STATIC/accounts/js/selected.jquey.js,或者无论您将服务器配置为使用Apache或nginxOK从何处获取文件,我尝试了上述操作,但我给出了以下错误:无法从“static/accounts/js/selected.jquery.js”解析其余部分:“static/accounts/js/selected.jquery.js”
<script src= "{% static ‘security/accounts/js/chosen.jquery.js’ %}" type="text/javascript"></script>