Retrieving Post Data in the View

Inside of the CreatePost view we need to do the following:

  1. retrieve the data sent to us in the POST request
  2. create a Post in our DB based on that data
  3. tell the client to reload the page to reflect the updated DB

In this section we'll just get the data from the POST request. As stated early, all the data and info from the user that is sent to us is contained in the request variable. You can access the POST request data by printing out request.POST:

class CreatePost(View):
    def post(self, request):
        print(request.POST)

If you enter values in the form and submit, you'll see them being printed out in a dictionary in the terminal running the server.

To get the specific fields that we want data from, remember that we have to use the keys that were defined when we gave the inputs specific names. To access the data do the following:

class CreatePost(View):
    def post(self, request):
        name = request.POST.get("name")
        url = request.POST.get("imgURL")
        caption = request.POST.get("caption")

Now that we have the data, let's make the database object in the next section!

results matching ""

    No results matching ""