Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: can i give an argument to a form to use it for different cases?
    text
    copied!<p>i'm new to Django and i use one form twice for user accounts (username, password). The first user is required, the second one is alternatively. </p> <p>The problem is, that the checkbox is displayed by the required, too. Can i give an argument to the view to avoid the checkbox by the first user and display it for a second user? I wanna do a case like</p> <p>Is that possible? I dont wanna write two different classes with the same fields just because one different field.</p> <p>My code:</p> <p>views.py</p> <pre><code>if request.method == "POST": admin_form = MysqlDatabaseForm_user(request.POST or None, given_argument='admin') user_form = MysqlDatabaseForm_user(request.POST or None, given_argument='extra_user') if admin_form.is_valid(): #do something if user_form.is_valid(): #do something else: admin_form = MysqlDatabaseForm_user() user_form = MysqlDatabaseForm_user() return TemplateResponse(request, "services/database/add_service_database.html", {'admin_form': admin_form, 'user_form': user_form},) </code></pre> <p>forms.py</p> <pre><code>class MysqlDatabaseForm_user(forms.ModelForm): class Meta: model = MysqlUser def __init__(self, *args, **kwargs): given_argument = kwargs.pop('given_argument', None) super(MysqlDatabaseForm_user, self).__init__(*args, **kwargs) if given_argument == 'extra_user': self.fields['extra_user'] = forms.BooleanField(required=False) else: self.fields[given_argument] = forms.BooleanField(required=False) </code></pre> <p>The else-case is for screening, what is in "given_argument". it seems to be empty</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload