title: How can i store sensitive data in a Django project?
body: <p>I'm building a project where i will be able to save an API key, then this key will be used to perform some operations on my site from an external python bot.</p>
  
  <p>Obviously, no one should access the key, except my code that will perform some tasks using those keys. </p>
  
  <p>At the actual moment, the keys are stored on my database and they are not encrypted, but now i would like to make this system as safe as possible, so that even if someone has access to my credentials, he can't use it.</p>
  
  <p>The problem is that i know very little about security and encription. My first thought was to encrypt the keys, but i don't really know how to do that. </p>
  
  <p>Basically, that's how it should work: the keys are submitted using a form -> the keys must be encrypted and saved on my db -> later on, when i need to perform a task, those keys should be decrypted in order to do what i need</p>
  
  <p>Is my approach correct? And how can i accomplish that in Django?</p>
  
  <p>Here is my current view:</p>
  
  <pre><code>def homepage(request):
      if request.method == 'POST':
          # create a form instance and populate it with data from the request:
          form = ApiForm(request.POST)
          # check whether it's valid:
          if form.is_valid():
              # process the data in form.cleaned_data as required
              send = form.save()
              send.save()
              messages.success(request, f"Success")
  
      else:
          form = ApiForm()
  
      return render(request,
                    "main/home.html",
                    context={"form":form})
  </code></pre>
  
  <p>And here is the form:</p>
  
  <pre><code>class ApiForm(forms.ModelForm):
  
      key = forms.CharField(widget=forms.TextInput(
          attrs={
              'placeholder': 'Enter here your api key',
              }
          ), #max_length=9
      )
  
      secret = forms.CharField(widget=forms.TextInput(
          attrs={
              'placeholder': 'Enter here your secret key',
              }
          ), 
      )
  
  
      class Meta:
          model = MyModel
          fields = ("key", "secret")
  
      def save(self, commit=True):
          send = super(ApiForm, self).save(commit=False)
          if commit:
              send.save()
          return send
  </code></pre>
  
  <p>Keep in mind that those API keys will be used by an external Python scrypt that will perform some operations, so they need to be decrypted by that script.</p>