开发者

Django form prefix with class based generic view

开发者 https://www.devze.com 2023-04-01 03:37 出处:网络
How can I set the form prefix keyword with the new class-based generic views in Django 1.3?Se开发者_运维百科tting the prefix prevents duplicate id in the rendered HTML.This is documented here for the

How can I set the form prefix keyword with the new class-based generic views in Django 1.3? Se开发者_运维百科tting the prefix prevents duplicate id in the rendered HTML. This is documented here for the forms API.

Since I have several different forms loaded using AJAX, I am running into problems with duplicate tags.


I see your problem! You're trying to use FormView, which lets you specify form_class, but not a prefix.

I haven't tried this, but I suggest subclassing FormView, or whatever you're using, and overriding the get_form_kwargs method to add the prefix in.

You could do something like this:

class MyFormView(FormView):
    form_prefix = None

    def get_form_kwargs(self):
        kwargs = super(FormView, self).get_form_kwargs()
        if self.form_prefix:
            kwargs.update({'prefix': self.form_prefix})
        return kwargs

Then, I think, you'll be able to put form_prefix in the arguments to MyFormView.as_view in your urlconf.

As I say, I haven't tried this, but it might be worth a go - let me know if it works!


I opened a ticket in django, with a patch attached, that modifies FormMixin, in order to make FormView behave like you wish.

https://code.djangoproject.com/ticket/18872

0

精彩评论

暂无评论...
验证码 换一张
取 消