Django form not saving to database

Nate Thompson
Nate Thompson used Ask the Experts™
So, I've been working on a Tic Tac Toe app. (Trying to learn how to user django/python)
All of my code seems to be functional, but the issue is that when the user submits the form, the data doesn't seem to go into the database at all. Because it doesn't find its way into a db table, I can't use that value on another page.
Could anyone review my code and see if you notice anything wrong?
Thanks in advance! -
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, render_to_response
import os
from .forms import PlayerInfo
from .models import Player

def index(request):
    return render(request, 'index.html')

def start(request):
    if request.method == 'POST':
        form = PlayerInfo(request.POST)
        if form.is_valid():
            obj = Player()
            obj.player_one_name = form.cleaned_data['player_one_name']
            obj.player_two_name = form.cleaned_data['player_two_name']
            return HttpResponseRedirect('game/')

        form = PlayerInfo()

        return render(request, 'start.html')

def game(request):
    queryset = Player.objects.all()

    args = {'fpn': queryset.values('player_one_name'), 'spn': queryset.values('player_two_name')}
    return render_to_response('game.html', args)

Open in new window -
from django.db import models

class Player(models.Model):
    player_one_name = models.CharField(max_length=30, default='')
    player_two_name = models.CharField(max_length=30, default='')

Open in new window -
from django import forms

class PlayerInfo(forms.Form):
    player_one_name = forms.CharField(max_length=30, label='First player name')
    player_two_name = forms.CharField(max_length=30, label='Second player name')

Open in new window

start.html -
{% extends 'base.html' %}
{% block botRow %}
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
{% endblock %}

Open in new window

game.html -
{% extends 'base.html' %}
{% block midRow %}
    <p>{{ fpn }}</p>
    <p>{{ spn }}</p>
{% endblock %}

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr Software Engineer

look at that link.  been awhile since I've actually used the forms, but you need to pass the form variable to the template via the context parameter (you're missing that)

also, you're not doing anything with the obj instance you created.  you'd need to add

finally, you do a and get rid of the or you get rid of the obj lines altogether and modify your Form class to define the class Meta area to tie it to the Player model class you defined.  If you do that, then will work
Cyclops3590Sr Software Engineer

I had copied/tested the given code so I know my recommendation works as the author is desiring

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial