0

I have created my first app in Django (1.10.5) / Python 3.4. I have a login page and a register page. Which is working fine.

I can create new user and login with that id. Now after the login I want user to fill a form with some information and click on submit. And the information should get stored in the database.

So I created a model first : Model.py

class UserInformation(models.Model):
     firstName = models.CharField(max_length=128)
     lastName = models.CharField(max_length=128)
     institution = models.CharField(max_length=128)
     institutionNumber = models.CharField(max_length=128)
     cstaPI = models.CharField(max_length=128)
     orchidNumber = models.CharField(max_length=128)

This has created a table in the DB.

forms.py

class UserInformationForm(ModelForm):
    class Meta:
        model = UserInformation
        fields = '__all__'

views.py

def home(request):
    form = UserInformationForm()
    variables =  { 'form': form, 'user': request.user }
    return render(request,'home.html',variables)

home.html

{% extends "base.html" %}
{% block title %}Welcome to Django{% endblock %}
{% block head %}Welcome to Django{% endblock %}
{% block content %}
    <p> Welcome {{ user.username }} !!! <a href="/logout/">Logout</a><br /><br /> </p>   

    <form method="post" action=".">{% csrf_token %}
        <table border="0">
            {{ form.as_table }}
        </table>
    <input type="submit" value="Submit"  style="position:absolute"/>

    </form>

{% endblock %}

But when I click on submit button, It does not insert data into my table.

6
  • You are not receiving the information on the view. How do you expect it to insert it into the database ? Read more on request.POST handling in Django
    – karthikr
    Commented Jan 23, 2017 at 2:17
  • I just want this information to be in my database.. I thought it will be straight forward. Commented Jan 23, 2017 at 2:45
  • Yes, it is straight forward, but you need to handle it in the view. Read more here: docs.djangoproject.com/en/1.10/topics/forms/#the-view
    – karthikr
    Commented Jan 23, 2017 at 2:48
  • Thanks, Values are getting stored now. Thanks a lot. Commented Jan 23, 2017 at 3:04
  • There you go.. Good job
    – karthikr
    Commented Jan 23, 2017 at 3:15

2 Answers 2

1

here is the answer, we need to use the request.POST

def home(request):
    if request.method == 'POST':
        form = UserInformationForm(request.POST)
        form.save()
        return HttpResponseRedirect('/home/')
    else:
        form = UserInformationForm()
        variables =  { 'form': form, 'user': request.user }

    return render(request,'home.html',variables)
0

the first: you need add urls.py to you app the second: you need to change your views.py to lool like this

`
    info = UserInformation()
    lastName = request.POST.get('lastName')
    ...
    info.save()
`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.