Tuesday, December 24, 2019

Re: send data to database

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.


On Tue, Dec 24, 2019 at 6:20 AM Leó Horváth <horvath.leo2001@gmail.com> wrote:
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ó 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJBcW0xearS3cj4XD%2BT0HEOCTvSSFMeJ18dvkjMaPdzwVfXf0w%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANips4KVWWx%3DECgM37CDSwpDoKJ3KBhFREhw4H8qZcuCDgbD7g%40mail.gmail.com.

No comments:

Post a Comment