1# =======================================================
2# Non-Model Forms - use 'initial' and supply a dictionary
3
4# views.py
5form = YourForm(initial={'charfield1': 'foo', 'charfield2': 'bar'})
6
7# forms.py
8class YourForm(forms.Form):
9 charfield1 = forms.CharField(max_length=3)
10 charfield2 = forms.CharField(max_length=3)
11 choicefield = forms.ModelChoiceField(MyModel.objects.all())
12
13# ======================================================================
14# Model Forms - use 'instance' and supply with an instance of your model
15
16# models.py - Create Model
17class Office(models.Model):
18 location = models.CharField(max_length=30)
19
20# forms.py - Create Model Form
21class OfficeForm(forms.ModelForm):
22 class Meta:
23 model = Office
24 fields = '__all__'
25
26# views.py -
27def offices(request):
28 office = Office.objects.get(pk=request.user.office.id)
29 form = OfficeForm(instance=office)
30 return render(request, '.../index.html', {'form':form})