def __init__(self, data=None, files=None, instance=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList, label_suffix=':', empty_permitted=False): if not instance: raise NotImplementedError("Ad instance must be provided") self.instance = instance object_data = self.instance.fields_dict() self.declared_fields = SortedDict() self.base_fields = fields_for_ad(self.instance) # if initial was provided, it should override the values from instance if initial is not None: object_data.update(initial) BaseForm.__init__(self, data, files, auto_id, prefix, object_data, error_class, label_suffix, empty_permitted)
def bootstrap_form(form: BaseForm): if not issubclass(form.__class__, BaseForm): raise ValueError(_('本模板标签不能用于%s。') % form.__class__) form.required_css_class = 'font-weight-bold' form.error_css_class = 'text-danger' for field_name in form.fields: form.fields[field_name].widget.attrs.update({ 'class': 'form-control', 'placeholder': _('请输入%s') % form.fields[field_name].label }) return form
def __init__(self, instance, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList, label_suffix=':', empty_permitted=False): self.instance = instance object_data = ad_to_dict(self.instance) self.declared_fields = SortedDict() self.base_fields = fields_for_ad(self.instance) # if initial was provided, it should override the values from instance if initial is not None: object_data.update(initial) BaseForm.__init__(self, data, files, auto_id, prefix, object_data, error_class, label_suffix, empty_permitted)
def get_serialized_value(Type: Type["FormType"], value: django_forms.BaseForm, schema: Thing) -> JSON: form = value context = form.get_context() # type: ignore[attr-defined] hidden_fields = { field.name: field for field in context["hidden_fields"] } visible_fields = {field.name: field for field, _ in context["fields"]} # TODO: hackey way to make bound fields work. # This creates a property that is then accessible by our serializer # directly, giving us bound fields instead of unbound fields. The # proper way to do this is to make fields a mapped type of unbound # fields, and then unbound field a type that has a .field property for # the bound field. value.fields = {**hidden_fields, **visible_fields} serialized = serialize(value, schema, suppress_custom_serializer=True) serialized[ "name"] = f"{value.__class__.__module__}.{value.__class__.__qualname__}" serialized["prefix"] = form.prefix or "" serialized["iterator"] = list(hidden_fields.keys()) + list( visible_fields.keys()) serialized["hidden_fields"] = list(hidden_fields.keys()) serialized["errors"] = form.errors or None return serialized """
def form_invalid(self, form: forms.BaseForm) -> HttpResponse: # The MDC Snackbar used for message display strips HTML from the label, # so just flatten this to plain text for now. message = format_html("{} {}", form.errors.as_text(), form.non_field_errors().as_text()) assert isinstance(self.request, WSGIRequest), self.request messages.add_message(self.request, messages.ERROR, message) return super().form_invalid(form)
def form_valid(self, form: BaseForm) -> HttpResponse: assert isinstance(form, PlayerForm) player = form.create_player() response = super().form_valid(form) response.set_cookie( key=str(self.kwargs['game_id']), value=str(player.id), expires=get_expiry(), ) return response
def form_valid(self, form: BaseForm) -> HttpResponse: assert isinstance(form, GameForm) self.game = form.create_game() response = super().form_valid(form) response.set_cookie( key=str(self.game.id), value=str(self.game.players[0].id), expires=get_expiry(), ) return response
def form_valid(self, form: BaseForm): obj = form.save(commit=False) obj.post_id = get_object_or_404(Post, post_id=self.kwargs.get('pk')) try: obj.user = self.request.user obj.save() except Exception as E: print(E) form._errors[forms.forms.NON_FIELD_ERRORS] = ErrorList( ["user must be logged in to continue"]) return super(PostDetailCommentCreateView, self).form_invalid(form) return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form: forms.BaseForm) -> None: # type: ignore form = cast(forms.ModelForm, form) form.save()
def save_form(form: BaseForm): form.full_clean() return form.save()