Avatar of Nate Thompson
Nate Thompson

asked on 

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

* DjangoPython

Avatar of undefined
Last Comment
Cyclops3590
ASKER CERTIFIED SOLUTION
Avatar of Cyclops3590
Cyclops3590
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Cyclops3590
Cyclops3590
Flag of United States of America image

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

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in other languages. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive set of standard libraries, including NumPy, SciPy, Django, PyQuery, and PyLibrary.

6K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo