0

小部件.py

from django.utils.safestring import mark_safe
from django.forms.widgets import Widget, Select
from django import forms
from django.conf import settings


class CheckboxDivSelectMultiple(CheckboxSelectMultiple):
'''renders the checkboxes as divs with a hard coded class'''

def render(self, name, value, attrs=None, choices=()):
    if value is None: value = []
    has_id = attrs and 'id' in attrs
    final_attrs = self.build_attrs(attrs, name=name)
    output = [u'<div>']
    """""""""
        option_label = conditional_escape(force_unicode(option_label))
        output.append(u'<div class="%s"><label%s>%s %s</label></div>' % ('new-class', label_for, rendered_cb, option_label))
    output.append(u'</div>')
    return mark_safe(u'\n'.join(output))

表格.py

class TypeSelectionForm(forms.Form):
    checkbox_field = forms.MultipleChoiceField(widget=forms.CheckboxDivSelectMultiple(), label="", required=False)

我正在用自定义小部件覆盖 checkboxselectmultiple。我将小部件代码放在 widgets.py 中。返回错误“ViewDoesNotExist at /setting/type/”

下面回溯:

Environment:

Request Method: GET
Request URL: http://192.168.100.12/setting/type/

Django Version: 1.4.1
Python Version: 2.7.0

Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  300.                     sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  300.                     sub_match = pattern.resolve(new_path)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  209.             return ResolverMatch(self.callback, args, kwargs, self.name)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in callback
  216.         self._callback = get_callable(self._callback_str)
File "/usr/lib/python2.7/site-packages/django/utils/functional.py" in wrapper
  27.         result = func(*args)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable
  101.                     (lookup_view, mod_name))

Exception Type: ViewDoesNotExist at /setting/type/
Exception Value: Could not import setting.views.types. View does not exist in module setting.views.
4

1 回答 1

0

根据您的进口,这行不应该:

class CheckboxDivSelectMultiple(CheckboxSelectMultiple):

是这个吗?

class CheckboxDivSelectMultiple(forms.CheckboxSelectMultiple):
于 2013-09-13T23:11:24.120 回答