0

I am fairly new to Django and I have set up a sign up form with some additional fields.

In my forms.py file I have the following code:

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )

In my views.py I have the following code:

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('index')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})

Then in my signup.html view I am rendering the following:

<form method="post">
    {% csrf_token %}
    {% for field in form %}
      <p>
        {{ field.label_tag }}<br>
        {{ field }}
        {% for error in field.errors %}
          <p style="color: red">{{ error }}</p>
        {% endfor %}
      </p>
    {% endfor %}
    <button class="btn-contact" type="submit">Let's Go!</button>
  </form>

Essentially what I want to do is add placeholder text in each field but this is not appearing the way I have set it up.

To do this I need more control over the forms fields but I don't know how to do that because of the way I am rendering fields in a loop.

How would I go about doing this?

Adam Dalton
  • 111
  • 1
  • 1
  • 10

1 Answers1

0

For placeholders, take a look at How do I add a placeholder on a CharField in Django?

One good advice that I have to give you, is that you shouldn't manually render your form, because Django has mechanisms to do that quite well (like {{form.as_p}} in your case). See Working with forms.

nyr1o
  • 966
  • 1
  • 9
  • 23