1

What I'm trying to do is to allow a user to enter any text in a form and have the data stored in a database. However, it is not being saved when I test it. I'm sure it's probably something stupid, but any help will be appreciated. I'm also pretty new to using Django.

Below is what I have currently have. Any help will be appreciated.

models.py:

from __future__ import unicode_literals

from django.db import models

class TEST(models.Model):
    test_name = models.CharField(max_length=100)

forms.py:

from django import forms

class Test_Form(forms.Form):
    test_name = forms.CharField(max_length=100)

views.py:

from django.shortcuts import render
from Test.forms import Test_Form
from Test.models import Test
from django.http import HttpResponseRedirect

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

def Test_View(request):
    if request.method == 'POST':

        form = Test_Form(request.POST)
        if form.is_valid():
                test_name = request.POST.get('test_name', '')

        return HttpResponseRedirect(reverse('Test:IOC'))

    else:
        form = Test_Form()

    return render(request, 'Test/Test.html', {'form': form})

Snippet from test.html

<form action="/Test/" method="POST" id="Test" class="form-horizontal form-groups-bordered"     role="form">
{% csrf_token %}
<div class="form-group">

<div class="row">
    <label class="col-lg-2 control-label" for="test_title">Full Name of Test</label>
    <div class="col-lg-8">
        <input id="ioc_name" class="form-control" name="test_name" type="CharField" data-validate="required" placeholder="ex: This is a test">
</div>

<div class="col-lg-1 col-lg-offset-9">
    <a class="btn btn-success btn-icon">
    Submit
        <input type="submit" />
        <i class="entypo-check"></i>
    </a>
</div>
</form>
1
  • Why do you think this code should save anything? Where are you doing anything to cause data to be saved? Commented Feb 18, 2017 at 15:19

1 Answer 1

1

The model instance isn't save automatically with magic. You need to instantiate it, assign the data, and call the save method, like this:

test = TEST(test_name=form.cleaned_data["test_name"])
test.save()

Or in one step: TEST.create(test_name=form.cleaned_data["test_name"])

Or even shorter (if I remember well): TEST.create(**form.cleaned_data)

You should check the docs from creating forms from models, it'll get your work easier (this is for Django 1.1.0) (https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/)

Also, your input is wrong:

  1. You have to set type to a valid value (text in this case). Anyway, as t doesn't recognizes CharField as a valid type, it sets its value to text, which is the default, so you don't have problems here. Valid types
  2. I don't know if data-validate is part of your own code, but if not and you wanna the field be required before hitting submit, you should use required, which is an HTML attr.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.