【问题标题】:struggling with Django Chosen Form与 Django 选择的表格作斗争
【发布时间】:2018-01-04 17:17:57
【问题描述】:

我是一名新开发人员,我遇到了一个名为 Chosen.js 的 JavaScript 库,它非常适合我的学校高级项目。我的问题是我在开始使用 django-chosen 时遇到了问题。我认为这与 linting 工具中显示的无效换行符有关,我还遗漏了其他内容。

在 pip install django-chosen 之后,我将选择添加到 settings.py 中的已安装文件中,如下所示:

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 中找到它。我该如何更改?

【问题讨论】:

    标签: django jquery-chosen


    【解决方案1】:

    您的 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 的任何路径。

    【讨论】:

    • 如何找到我的 STATIC_ROOT?现在,我将文件移动到 Security/Accounts/Static/Accounts。我的 STATIC URL 定义为 /static/,这是您引用的根目录吗?更改后出现以下错误:django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 60: 'static', expected 'endblock'。您是否忘记注册或加载此标签? [04/Jan/2018 15:16:18] "GET /account/profile/HTTP/1.1" 500 198992
    • 第 60 行定义为:
    • 您需要阅读静态文件(查看 Django 文档)。靠近模板顶部的 {% load static %} 将解决此错误
    • 暂时忘记 STATIC_ROOT,那是为您部署的应用程序。 STATIC_URL 告诉 Django 如何创建 URL,在您的情况下,它将成为 /static/accounts/js/chosen.jquey.js 相对于您的项目根目录(使用 runserver)或您配置服务器以从中获取文件的任何位置(使用 Apache 或nginx)
    • 好吧,我尝试了上面的方法,但我给了我以下错误:无法解析剩余部分:''static/accounts/js/chosen.jquery.js'' from ''static/accounts /js/chosen.jquery.js'
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多