title: Using Django and Postgres to store phone numbers
body: <p>I currently want to make a web app in django with a form that asks a user to input a phone number and submit, and I want that number they submit to be stored in a database using postgres. I'm having a hard time finding information on how to use post requests in django to connect to postgres.</p>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
    &lt;title&gt;Test Form 1&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form method="post"&gt;{% csrf_token %}
      {{ form.as_p }}
      &lt;input type="submit" value="Send message"&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>admin.py</strong></p>
  
  <pre><code>from django.contrib import admin
  from .models import Post
  
  admin.site.register(Post)
  </code></pre>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>from django.contrib import admin
  from django.urls import path
  # from main.views import first
  from main import views as test_app_views
  
  urlpatterns = [
      path('admin/', admin.site.urls),
      # path('', first)
      path('', test_app_views.FormView1.as_view())
  ]
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>from django import forms
  from phone_field import PhoneField
  from main.models import Post
  
  class HomeForm(forms.ModelForm):
      phone = PhoneField()
  
      class Meta:
          model = Post
          fields = ('phone',)
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from django.db import models
  from phone_field import PhoneField
  from django.contrib.auth.models import User
  
  
  class Post(models.Model):
      phone = PhoneField()
      user = models.ForeignKey(User, on_delete=models.CASCADE,)
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from django.shortcuts import render
  from django.views.generic.edit import FormView
  from .forms import HomeForm
  
  class FormView1(FormView):
      template_name = 'index.html'
      # form_class = RequiredInputsForm
      # success_url = '/index/success/'
  
      def get(self, request):
          form = HomeForm()
          return render(request, self.template_name, {'form': form})
  
      def post(self, request):
          form = HomeForm(request.POST)
          if form.is_valid():
              post = form.save(commit=False)
              post.user = request.user
              post.save()
  
              phone = form.cleaned_data['post']
              form = HomeForm()
              return redirect('index:index')
  
              args = {'form': form, 'text': text}
              return render(request, self.template_name, args)
  </code></pre>
  
  <p>How can I get this to store the phone number in the postgres db after I've initialized the project to use it? This was what I tried but I am consistently getting the error: 'ValueError at /
  Cannot assign ">": "Post.user" must be a "User" instance.'</p>
  
  <p>I think what I'm trying to do is quite simple, I've just never done it before with django and postgres, any help would be appreciated!</p>