Django Forms vs Angularjs

324 views
Skip to first unread message

Gorkem Tolan

unread,
Mar 18, 2016, 12:38:59 PM3/18/16
to Django users
I am a new comer to Django. Last four weeks I have been working on web application purely created with Django framework. 
I realized that Django forms are very cumbersome to use. 
I'd rather have DRF (rest framework) and just angularjs form. I am sure alot users will bombard me tons of oppositions. 
I'd like to hear the oppositions and critics maybe I am missing some points and knowledge. 
Thanks,

bobhaugen

unread,
Mar 19, 2016, 9:24:57 AM3/19/16
to Django users
Questions interspersed below:


On Friday, March 18, 2016 at 11:38:59 AM UTC-5, Gorkem Tolan wrote:
I am a new comer to Django. Last four weeks I have been working on web application purely created with Django framework. 
I realized that Django forms are very cumbersome to use. 

What is cumbersome about them? Can you post some of your form code that you find cumbersome? Maybe you are doing something that is more complicated than it needs to be.

My experience with Django forms is that they get something up and running very quickly and very easily and run into problems when I try to do something more complicated. And then the main problem for me is if I have a lot of forms on the same page: the page starts to render very slowly. But the code is still fairly simple.
 
I'd rather have DRF (rest framework) and just angularjs form. I am sure alot users will bombard me tons of oppositions. 

DRF is awesome!  If you are more familiar with Angular than Django, it might be a good way for you to go.

Gorkem Tolan

unread,
Mar 22, 2016, 9:17:54 AM3/22/16
to Django users
Thanks for your response. Currently I am dealing with a form with individual field permissions. I guess it explains it gets difficult with Django Form, which are made for only editing. I know a custom read-only field can be created in the code below.  I am still struggling with post the form with labels not fields. As I newcomer to Django, it's very convenient to have these capabilities on a regular with simple form permissions. But once it gets bit more complicated in security, Form object is not very helpful or i maybe am missing a point. 

from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.forms.util import flatatt

class ReadOnlyWidget(forms.Widget):
    def render(self, name, value, attrs):
        final_attrs = self.build_attrs(attrs, name=name)
        if hasattr(self, 'initial'):
            value = self.initial
            return mark_safe("<p %s>%s</p>" % (flatatt(final_attrs), escape(value) or ''))

    def _has_changed(self, initial, data):
        return False

class ReadOnlyField(forms.Field):
    widget = ReadOnlyWidget
    def __init__(self, widget=None, label=None, initial=None, help_text=None):
        super(type(self), self).__init__(self, label=label, initial=initial, 
            help_text=help_text, widget=widget)
        self.widget.initial = initial

    def clean(self, value):
        return self.widget.initial

bobhaugen

unread,
Mar 22, 2016, 9:31:43 AM3/22/16
to Django users
Maybe you already know this, but you can do a lot of form tinkering in __init__, like so:

    def __init__(self, permissions_parameter=None, *args, **kwargs):
        super(FormName, self).__init__(*args, **kwargs)
        if permissions_parameter:
            #do a bunch of form tinkering

Gorkem Tolan

unread,
Mar 22, 2016, 9:57:17 AM3/22/16
to Django users
Actually I do to set permission to each field. A field in the form can be viewable and editable, only viewable, or hidden.  So if user has a permission to see the form, but edit some fields in the form, it gets very tricky especially for validation.
For example I used these statements to make readonly field :
self.fields[name].widget.attrs['disabled'] = 'disabled'
self.fields[name].widget.attrs['readonly']=True
Then the problem rises not to be able send disabled fields in the post. 

Fabio C. Barrionuevo da Luz

unread,
Mar 22, 2016, 10:36:34 AM3/22/16
to django...@googlegroups.com

self.fields[name].widget.attrs['disabled'] = 'disabled'
self.fields[name].widget.attrs['readonly']=True

is not make real readonly to field, because if user can edit the html on client side, and remove disabled="disabled" and readonly input atributtes

to problem of readonly fields, i currently use this:

https://github.com/luzfcb/django-simple-history/blob/wip-generic-views2/simple_history/forms.py


I prevent it here https://github.com/luzfcb/django-simple-history/blob/wip-generic-views2/simple_history/forms.py#L24

Usage:

class FooBar(models.Model):
    foo = models.TextField(blank=True)
    bar = models.TextField(blank=True)

class MyFooBarForm(forms.ModelForm):
    class Meta:
        object = FooBar
        fields = ('foo', 'bar')

class MyReadOnlyBarForm(ReadOnlyFieldsMixin, forms.ModelForm):
    readonly_fields = ('bar')
    class Meta:
        object = FooBar
        fields = ('foo', 'bar')

my_all_field_readonly_foobarForm = new_readonly_form_class(MyFooBarForm)

my_foo_field_readonly_foobarForm = new_readonly_form_class(MyFooBarForm, readonly_fields=('foo',))


I do not know if this is the best approach, but it was what I got so far.





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/877bb8f8-4b59-4353-a0e5-99e95f55811b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

Gorkem Tolan

unread,
Mar 23, 2016, 9:39:45 AM3/23/16
to Django users
Fabio,

I appreciate for your contribution. I understand the point that create a separate form with only readonly fields. However, I have a form has a mix of fields (readonly and editable) based on the user permission, not only one type of field. Also, When readonly field is defined by disabled=true, the field information doesn't get sent during the post. 

Daniel Hepper

unread,
Mar 26, 2016, 8:49:17 AM3/26/16
to Django users
Fabio,

if you are using Django 1.9, you can use the newly introduced disabled attribute on forms.Field, see https://docs.djangoproject.com/ja/1.9/ref/forms/fields/#disabled

Gorkem Tolan

unread,
Mar 28, 2016, 9:17:59 AM3/28/16
to Django users

I was working on it last couple of days. Basically I came up with what Fabio's solution. Thanks Daniel for 'disabled' field comment as well.  

Fred Stluka

unread,
Apr 2, 2016, 5:16:47 PM4/2/16
to django...@googlegroups.com
Fabio,

Good point!

Browser-side security is VERY easy to bypass.  

For example, just use Firebug or the built-in dev tools of Firefox,
Chrome, or Safari (or probably even IE by now), to edit the HTML
of the current page and then click the OK/Send/Submit button.

--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
Reply all
Reply to author
Forward
0 new messages