与 Django Chosen Form 作斗争
struggling with Django Chosen Form
我是一名新开发人员,我遇到了一个名为 Chosen.js 的 javascript 库,它非常适合我的学校高级项目。我的问题是我在开始使用 django-chosen 时遇到了问题,无法显示所选的表单。我认为这与 linting 工具中显示的无效换行符有关,我还遗漏了其他内容。
在 pip install django-chosen 之后,我在 settings.py 中将 chosen 添加到我安装的文件中,如下所示:
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();
}
)
;
我觉得好像少了一步,网上的教程大多与 django_admin 相关,而且似乎不完整。任何让我起床和 运行 的建议都将不胜感激。
To expand on this i'm now getting the following error after adjusting my script tags.
[04/Jan/2018 14:20:19] "GET /account/profile/chosen.jquery.js HTTP/1.1" 404 2849
Not Found: /account/profile/chosen.jquery.js
[04/Jan/2018 14:20:19] "GET /account/profile/chosen.jquery.js HTTP/1.1" 404 2849
[04/Jan/2018 14:20:22] "GET /account/profile/ HTTP/1.1" 200 20109
Not Found: /account/profile/chosen.jquery.js
[04/Jan/2018 14:20:22] "GET /account/profile/chosen.jquery.js HTTP/1.1" 404 2849
出于某种原因,当文件位于 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>
或者您的脚本相对于您的保存路径 STATIC_URL。
我是一名新开发人员,我遇到了一个名为 Chosen.js 的 javascript 库,它非常适合我的学校高级项目。我的问题是我在开始使用 django-chosen 时遇到了问题,无法显示所选的表单。我认为这与 linting 工具中显示的无效换行符有关,我还遗漏了其他内容。
在 pip install django-chosen 之后,我在 settings.py 中将 chosen 添加到我安装的文件中,如下所示:
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();
}
)
;
我觉得好像少了一步,网上的教程大多与 django_admin 相关,而且似乎不完整。任何让我起床和 运行 的建议都将不胜感激。
To expand on this i'm now getting the following error after adjusting my script tags.
[04/Jan/2018 14:20:19] "GET /account/profile/chosen.jquery.js HTTP/1.1" 404 2849
Not Found: /account/profile/chosen.jquery.js
[04/Jan/2018 14:20:19] "GET /account/profile/chosen.jquery.js HTTP/1.1" 404 2849
[04/Jan/2018 14:20:22] "GET /account/profile/ HTTP/1.1" 200 20109
Not Found: /account/profile/chosen.jquery.js
[04/Jan/2018 14:20:22] "GET /account/profile/chosen.jquery.js HTTP/1.1" 404 2849
出于某种原因,当文件位于 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>
或者您的脚本相对于您的保存路径 STATIC_URL。