0

I'm having trouble passing values from my template to view. Basically I created a form for user registration, and want to pass the values from the template to view so that I can construct a User object to add to my database. But somehow my "input" is not working when I add name={{user_form.username}}. Also, I want to pass values 0,1,2 respectively when I select "borrower", "libarrian" and "clerk", what can I do to implement this?

Below are my codes.

      <form id="user_form" method="post" action="/sign_up/" class="form-signin" role="form">
          {% csrf_token %}
        <h2 class="form-signin-heading">Signup </h2>

            {% if error%}

        <div class="error"> Your registration has been unsuccessfull </div> 
        {% endif %}

        <input type="text" class="form-control" name="username" value="{{user_form.username}}" placeholder="Username" required autofocus>
        <input type="password" class="form-control" name="password1" placeholder="Password" value="" required>
        <input type="password" class="form-control" name="password2" placeholder="Retype password" value="" required>       


        <select class="form-control">
  <option value="2">Librarian</option>
  <option value="0">Borrower</option>
  <option value="1">Clerk</option>
</select>

        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button>

      </form>

In forms.py

class UserForm(forms.Form):
    username = forms.CharField(max_length=30)
    password1 = forms.CharField(widget=forms.PasswordInput())
    password2 = forms.CharField(widget=forms.PasswordInput())

In views.py

   def sign_up(request):
    registered = False;
    error = False;
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        if user_form.is_valid():
            registered = True
            username = user_form.cleaned_data['username']
            password = user_form.cleaned_data['password']
            user = User.objects.create_user(username, None, password)
            user.save()
        else:
            error = True;
    else:
        user_form = UserForm()

    return render(request, 'books/sign_up.html',
                  {'user_form':user_form,
                   'registered':registered, 'error':error})
2
  • Can you provide the template rendering line - how do you pass the form object to the template? Commented Mar 25, 2014 at 5:37
  • I never heard any input control with type username.. is this a type or you named it like this? Commented Mar 25, 2014 at 5:40

1 Answer 1

1

Below form should work.

<form id="user_form" method="post" action="/sign_up/" class="form-signin" role="form">
    {% csrf_token %}
    <h2 class="form-signin-heading">Signup </h2>
    <input type="text" class="form-control" name="username"  value="{{user_form.username}}" placeholder="Username" required autofocus>
    <input type="password" class="form-control" name="password1" placeholder="Password" required>
    <input type="password" class="form-control" name="password2" placeholder="Retype password" required>      
    <select class="form-control">
        <option value="1">Librarian</option>
        <option value="0">Borrower</option>
        <option value="2">Clerk</option>
    </select>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button>
</form>

Changes I have done are -

  1. Change the names of username and password textboxes
  2. Change the type for username textbox
  3. Added value attribute in select control
Sign up to request clarification or add additional context in comments.

10 Comments

Why would changing to name="username" pass the value to user_form.username?
Value should be passed to value attribute. Now edited my answer.
I don't quite understand your comment. You never mentioned value attribute above.
please refresh your screen
This didn't work. After adding value="{{user_form.username}}", my signup page simply had " placeholder="Username" required autofocus> outside the input box.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.