Q:

Add HTML form values to models in Django

New in Django and I'm having problems adding form data to models. I can't read form values and add to models. I want to add Users to models with name and one subject. 
#models.py
from django.db import models

class User(models.Model):
    name = models.CharField(max_length=80)
    subject = models.CharField(max_length=120)

#views.py
from django.shortcuts import render,render_to_response
from models import User
from django.http import HttpResponseRedirect

def add_user(request):
    if request.method == 'POST':
        f = User(request.POST)
        if f.is_valid():
            name = f.cleaned_data['name']
            subject = f.cleaned_data['subject']
            f.save()
            return HttpResponseRedirect('index.html')
        else:
            f = User()
        return render_to_response('index.html')

    return render(request, 'index.html')

#index.html
<!DOCTYPE html>
<html>
<head>
    <title>Sistemas Web</title>
</head>
<body>
    <div>
        <h1>Add User</h1>
        <form action="add_user" method="POST">{% csrf_token %}
            <label>Name</label><br>
            <input id="name" type="text"><br>
            <input type="radio" id="subject">A<br>
            <input type="radio" id="subject">B<br>
            <input type="submit" value="Enviar">
        </form>
    </div>
</body>
</html>

Any help will be appreciated.

A:

You must use request.POST.get() for getting the attributes that are send by the url, and send to the
f = User(request.POST)the attributes for the model like u = User(name=name, subject=subject)
#views.py
def add_user(request):
    if request.method == 'POST':
        name = request.POST.get('name', '')
        subject = request.POST.get('subject', '')
        u = User(name=name, subject=subject)
        u.save()
        return render(request, 'index.html')

    return render(request, 'index.html')

On the template you´re missing the name of the input tag, without it nothing is send.
#index.html
<!DOCTYPE html>
<html>
<head>
    <title>Sistemas Web</title>
</head>
<body>
    <div>
        <h1>Add User</h1>
        <form action="add_user" method="POST">{% csrf_token %}
            <label>Name</label><br>
            <input name="name" type="text"><br>
            <input type="radio" name="subject" value="A">A<br>
            <input type="radio" name="subject" value="B">B<br>
            <input type="submit" value="Enviar">
        </form>

    </div>
</body>
</html>

Hope it helps

