send data to database

11 views
Skip to first unread message

Leó Horváth

unread,
Dec 24, 2019, 9:21:23 AM12/24/19
to django...@googlegroups.com
Hi guys, 

I want to create a form with radio buttons that sends data to a specific database field.  How can I do it? 

My models.py looks like this: 
import datetime
from django.db import models
from django.utils import timezone

# Create your models here.

class Name(models.Model):
    user_name = models.CharField(max_length=200)

    publication_date = models.DateTimeField('date published')

    age= models.IntegerField(default=0)

    prof_comm_score = models.IntegerField(default=1)
    prof_comm_status = models.IntegerField(default=1)

    teamwork_score = models.IntegerField(default=1)
    teamwork_status =  models.IntegerField(default=1)

    prod_knowledge_score =  models.IntegerField(default=1)
    prod_knowledge_status =  models.IntegerField(default=1)

    part_comp_score =  models.IntegerField(default=1)
    part_comp_status =  models.IntegerField(default=1)

    relations_score =  models.IntegerField(default=1)
    relations_status =  models.IntegerField(default=1)

    strategy_score =  models.IntegerField(default=1)
    strategy_status =  models.IntegerField(default=1)

    loyality_score =  models.IntegerField(default=1)
    loyality_status =  models.IntegerField(default=1)
    
    overall_score = models.IntegerField(default = 7)

    def __str__(self):
        return self.user_name
    def was_published_recently(self):
        return self.publication_date >= timezone.now() - datetime.timedelta(days=1)

Lets say i want 7 radio buttons to determine the value of prof_comm_score (which is an int ranging from 1 to 7)

My html file looks like this rn: 
<body>
        {%block content%}
            <h1>#1 Sales profession, communication</h1>
            <form action="" method="POST"></form>
            {% csrf_token %}
                <input type="radio" name="1" id = "1" value="7">
                <input type="radio" name="1" id = "2" value="6">
                <input type="radio" name="1" id = "3" value="5">
                <input type="radio" name="1" id = "4" value="4">
                <input type="radio" name="1" id = "5" value="3">
                <input type="radio" name="1" id = "6" value="2">
                <input type="radio" name="1" id = "7" value="1">


        {%endblock%}
    </body>



here is my git repo if needed: 

Thanks a lot, 
Leó 

Kevin Dublin

unread,
Dec 24, 2019, 11:47:07 AM12/24/19
to django...@googlegroups.com
Hi Leo,

You'll want to use a model form in your forms.py, create an instance of it in the views.py to pass 'form' as a context variable, and then just let the model form render in the html template using {{ form }}. Check the docs:


This is a pretty thorough guide:


Cheers!

Kevin


Kevin Dublin
Etched Press Editor
Follow me @parteverything

Happiness is a state of being, just be.
Believe it, and it will be.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJBcW0xearS3cj4XD%2BT0HEOCTvSSFMeJ18dvkjMaPdzwVfXf0w%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages