0

I am newbie in python and django and I am facing difficulty in storing various fields of html page into database. E.g, I have a html page which contains 5 fields and one submit button . On submitting the form, I want all values from html form should be stored in table of the given database. Please help me in this.

1

2 Answers 2

1
models.py

from django.contrib.auth.models import User
from django.db import models

class AllocationPlan(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=50)
    data = models.CharField(max_length=4096)
    total = models.DecimalField(max_digits=10, decimal_places=2)

forms.py

from django import forms
from django.forms import ModelForm
from app_name.models import AllocationPlan   

class AllocationPlanForm(ModelForm):
    class Meta:
        model = AllocationPlan

views.py

from django.shortcuts import render
from app_name.forms import AllocationPlanForm

def add(request):
    if request.method == 'POST':
        form = AllocatinPlanForm(request.POST)
        if form.is_valid():
            form.save()
return render(request, 'page.html', {
    'form': AllocationPlanForm()
})

 page.html

 <form method="post">{% csrf_token %}
     {% for field in form %}
     {{field}}
     <input type="submit" value="Submit"/>
     {% endfor %}
 </form>
Sign up to request clarification or add additional context in comments.

8 Comments

if i m doing this then i m getting the error "undefined variable AllocationPlan" in forms.py and also in views.py regarding AllocationPlanForm
This is only a sample, you can use your own model. Just follow the pattern
is there anything we need to import in forms.py because i m getting an error undefined variable in that if i m writing "model= model_name"
If your getting an error again, check the location where you put your files (models.py, forms.py ....).
i have a pure html form which takes input from user. I want all input values from user to store in mysql db on submitting. And i am not getting what to write in action of <form> tag used in my html form
|
1

You should approach this from the angle of models, which map the model's attributes to database fields and can handily be used to create a form. This is called Object-Relational Mapping.

Start by creating (or modifying) models.py in your app's folder, and declare the model there (Essentially the fields you want to be stored). As mentioned, see Django's tutorials for creating forms and model-form mapping.

2 Comments

I have created models.py which consists of all the field of the table. In java, we use getters and setters for storing the value from any html form. So can you give me an e.g, illustrating to store form field to db. Take any random name of fields in html form.
@Abhay, Sami has given you that information. Did you bother to follow the link to the modelform documentation? It is all described there.

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.