pythondjango-modelsdjango-viewscrud

Add Radio Button in gender feied in crud django


i have a CRUD PROJECT in which i need gender field in radio buttons or in check box i tried to apply those but did not getr the desired result, Radio buttons are not adding if they are adding they are not posting data in the database please can someone help and suggest me so way so i can apply in my crud , i am not using any form.py it is a simple crud using default models of django

My Html Page

<!DOCTYPE html>

</head>
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">

<body>
    <div class="container1">
        <h1>Add Member</h1>
        <form action="{% url 'user_data_create' %}" method="post">
            {% csrf_token %}
            <
            <select name="gender" id="gender" value="{{ user_data.gender }}" required>
                <option value="">-Select Gender-</option>
                <option value="Male" name="Male" >Male</option>
                <option value="Female" name="Female">Female</option>
                <option value="Other"name="Other">Other</option>
            </select> 

</html>

My Models . py

class Members(models.Model):
    GENDER_CHOICEs =(
    ('Male','Male'),
    ('Female','Female')
    )
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    name = models.CharField(max_length=20,null=True,blank=False)
    email = models.EmailField(null=True,blank=False,unique=True)
    phone = models.CharField(null=True,blank=False,max_length=10)
    gender = models.CharField(choices=GENDER_CHOICEs, max_length=10)
    role = models.CharField(max_length=10)
    is_deleted = models.BooleanField(default=False)

    def __str__(self):
        return f'{self.name} {self.email} {self.role}'

My views.py

def user_data_create(request):
        if request.method == 'POST':
            name = request.POST['name']
            email = request.POST['email']
            phone = request.POST['phone']
            gender = request.POST['gender']
            role = request.POST['role']
            print(gender)
            user_data = Members.objects.create(user=request.user, name=name, email=email, 
                                                phone=phone, gender=gender,role=role)
            user_data.save()
            messages.success(request,"New Member added Succesfully")
            return redirect('dashboard_page')
        return render(request, 'add.html')

Solution

  • For retrieve data you need to create another form

    views.py

    def HomePageView(request,id):
        data = Biodata.objects.get(id=id)
        context = {'data':data }
        return render(request, 'index.html', context)
    

    Html

    <div class="container position-absolute top-0 end-0 mt-5">
      <div class="row ">
        <div class="col-lg-5 p-3 border">
          <form action="" method="POST" novalidate>
            {% csrf_token %}
            <label for="male">Name</label><br>
            <input type="text" name="name" value="{{data.name}}" checked>
    
            <p>Please select Gender:</p>
    
    
            <input type="radio" name="gender"
              value="Male"
              {% if data.gender == 'Male' %} checked {% endif %}>
            <label for="male">Male</label><br>
    
            <input type="radio" name="gender" value="Female" {% if data.gender == 'Female' %} checked {% endif %}>
            <label for="female">Female</label><br>
            <input type="radio" name="gender" value="Other" {% if data.gender == 'Other' %} checked {% endif %}>
            <label for="other">Other</label> <br>
    
            <button class="btn btn-primary" type="submit">Submit</button>
          </form>
          <br>
          <br>
    
        </div>
      </div>
    </div>

    enter code here
    

    Output

    enter image description here