Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering csrfmiddlewaretoken in forms #40

Open
troygrosfield opened this issue Nov 10, 2013 · 1 comment
Open

Rendering csrfmiddlewaretoken in forms #40

troygrosfield opened this issue Nov 10, 2013 · 1 comment

Comments

@troygrosfield
Copy link

Currently, form rendering doesn't add the csrf token when rendering forms if you have django's CsrfViewMiddleware installed. It's kind of a pain in the butt to have to add that to every single form you want rendered. Why not check for installed apps and if CsrfViewMiddleware is installed, add the csrf_token by default since this will likely be the desired behavior? Then if you wanted, you could also create a form renderer that explicitly doesn't include the csrf hidden field when you don't want the csrf token:

{{ my_form|bootstrap_csrf_exempt }}

I don't mind doing the pull request for this, I just want to know why we wouldn't want to do this if there is a reason. What are other people's thoughts?

@troygrosfield
Copy link
Author

Unfortunately, this wasn't quite as easy as I might have hoped. Another path I went down was trying to make the rendering more django like with the "as_p", "as_table", etc by doing:

from django.utils.safestring import mark_safe

from bootstrapform.templatetags.bootstrap import bootstrap
from bootstrapform.templatetags.bootstrap import bootstrap_horizontal


class BootstrapFormMixin(object):

    def as_bootstrap(self):
        # TODO: add CSRF here.
        x = bootstrap(self).strip()
        return mark_safe(x)

    def as_bootstrap_horizontal(self):
        # TODO: add CSRF here.
        x = bootstrap_horizontal(self).strip()
        return mark_safe(x)

Then, in your templates you could just call:

{{ form.as_bootstrap }}

or

{{ form.as_bootstrap_horizontal }}

However, you don't have access to the csrf_token at that point. If you did, you could just add it to the form before rendering to html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant