Django form not saving to database

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!

views.py -
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']
            form.save()
            return HttpResponseRedirect('game/')

    else:
        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


models.py -
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


forms.py -
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>
    </form>
{% endblock %}

Open in new window


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

Open in new window

Nate ThompsonAsked:
Who is Participating?
 
Cyclops3590Connect With a Mentor Commented:
https://docs.djangoproject.com/en/2.0/topics/forms/

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 obj.save().

finally, you do a obj.save() and get rid of the form.save() 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 form.save() will work
0
 
Cyclops3590Commented:
I had copied/tested the given code so I know my recommendation works as the author is desiring
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.