0

I created a custom signup page.

I want the user to have, by default, two Boolean fields, is_nhs and agree_conditions.

However, the checked attribute in my HTML is not working. The database does receive it. In fact, if I log in as superuser in the Django dashboard, the attribute is_nhs and agree_conditions are always set to false (red cross).

My custom user model:

class CustomUser(AbstractUser):
  first_name = models.CharField(max_length=100, default='')
  last_name = models.CharField(max_length=100, default='')
  organization = models.CharField(max_length=100, default='')
  location = models.CharField(max_length=100, default='')
  postcode = models.CharField(max_length=100, default='')
  phone = models.CharField(max_length=100, default='')
  agree_conditions = models.BooleanField(default=True)
  is_nhs = models.BooleanField(default=False)

  def __str__(self):
    return self.email

My signup.html

 <form>
      <div class="form-group form-check">
        <input type="checkbox" class="form-check-input" id="exampleCheck1" checked="checked">
        <label class="form-check-label" for="exampleCheck1" name="agree_conditions">Agree conditions</label>
        <a href="{% url 'conditions'%}">Click here to see the conditions you are accepting</a>
      <div class="form-check" hidden>
      <input class="form-check-input" type="radio" name="is_nhs" id="exampleRadios1" value="Yes" required checked>
      <label class="form-check-label" for="exampleRadios1">
        Yes
      </label>
    </div>
      <button type="submit" class="btn btn-primary">Submit</button>
    </form>

I tried to change unsuccessfully to checked_attribute following this guide: What's the proper value for a checked attribute of an HTML checkbox?

Moreover, I find it strange that agree_conditions (in my model's field) shows a red cross (in the admin/superuser dashboard) even if it is by default=True.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prova12
  • 643
  • 1
  • 7
  • 25
  • 1
    Is any data arriving from your post? – Hagyn Jul 22 '19 at 17:28
  • Yes, `first_name`, `last_name` etc., they work. In fact, you cannot signup unless you fill all the fields, otherwise the signup page will reaload. Therefore, `is_nhs` and `agree_conditions` receive always `False` or the signup page will keep reloading. – Prova12 Jul 22 '19 at 17:33

1 Answers1

1

Your checkbox input does not have a name attribute; in fact, it looks like you've mistakenly put the name attribute on the label. It should be:

<input type="checkbox" class="form-check-input" name="agree_conditions" id="exampleCheck1" checked="checked">
<label class="form-check-label" for="exampleCheck1">Agree conditions</label>

(Note, you should really be using a Django form class for this.)

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks, m only concern about Django forms is that I do not understand clearly from the documentation how I can add boostrap etc. to them. – Prova12 Jul 23 '19 at 07:50