Note that there are some explanatory texts on larger screens.

plurals
  1. POSet form's default values from existing objects in Django
    primarykey
    data
    text
    <p>I'm trying to create a page of my <code>Django 1.5</code> project where users can change their presonal informations. I wrote a form:</p> <pre><code>class UserModify(forms.Form): middleschool = 'MS' highschool = 'HS' university = 'U' blank = '' male = 'M' female = 'F' mastercard = 'MC' maestro = 'M' visa = 'V' americanexpress = 'A' school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not Defined'),) sex = ((male, 'Male'), (female, 'Female'), (blank, 'Not defined'),) card_choices = ((mastercard, 'MasterCard'), (maestro, 'Mestro'), (visa, 'Visa/Visa Electron'), (americanexpress, 'American Express'),(blank, 'Not Defined')) password = forms.CharField(label = 'Password', widget=forms.PasswordInput) repassword = forms.CharField(label = ' Reinstert password', widget=forms.PasswordInput) name = forms.CharField(label = 'Name', required=False) surname = forms.CharField(label = 'Surname', required=False) school = forms.ChoiceField(choices = school_choices, required=False, label='What school are you enrolled?', initial = blank) birthdate = forms.DateField(label = 'Birth date', required=False, widget=forms.extras.SelectDateWidget(years=range(1950, 2015))) sex = forms.ChoiceField(choices = sex, required=False, label='Sex', initial = blank) city = forms.CharField(label = 'City', required=False) paypal_info = forms.EmailField(required=False,) card_type = forms.ChoiceField(choices = card_choices, label='Card type', required=False) card_info = forms.CharField(min_length = 16, max_length = 16, label = 'Your card number', required=False,) def clean_repassword(self): repassword = self.cleaned_data['repassword'] password = self.cleaned_data['password'] if repassword != password: raise forms.ValidationError("Verification password different from original password!") widgets = { 'password': forms.PasswordInput(), 'birthdate': forms.extras.SelectDateWidget(years=range(1950, 2014)) } </code></pre> <p>and this is the relative view:</p> <pre><code>def usermodify(request, user): mod_user = User.objects.get(username = user) if request.method == 'POST': form = UserModify(request.POST) if form.is_valid(): cd = form.cleaned_data mod_user.password = cd['password'] mod_user.first_name = cd['name'] mod_user.last_name = cd['surname'] mod_user.save() profile = mod_user.get_profile() profile.school = cd['school'] profile.birthdate = cd['birthdate'] profile.sex = cd['sex'] profile.city = cd['city'] profile.save() return render(request, 'user.html', {'request':request, 'view_user': mod_user, 'view_user_profile': profile}) else: form = UserModify() return render(request, 'user_modify.html', {'form': form, 'request': request, 'modify_user': mod_user}) </code></pre> <p>The form's rendering is as simple as it can get for now:</p> <pre><code>&lt;form action="" method="post"&gt; {% csrf_token %} {{ form.as_p }} &lt;input type="submit" value="Modify"&gt; &lt;input type="reset" value="Reset"&gt; &lt;/form&gt; </code></pre> <p>What I wantetd to do is populate the form by default with the current values in the object so that the User have to change only the one in which is interested and keep the others.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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