c.ceti
c.ceti

Reputation: 203

How to use form fields in template in django

i need your help, i'm trying to implement sign up functionality in django but i didn't know how to connect the template form to my view.

I'm using the following view :

def signup(request):
if request.method == 'POST':
    form = SignupForm(request.POST)
    if form.is_valid():
        user = form.save(commit=False)
        user.is_active = False
        user.save()
        current_site = get_current_site(request)
        subject = 'Activate your blog account.'
        message = render_to_string('acc_active_email.html', {
            'user':user, 'domain':current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': account_activation_token.make_token(user),
        })
        # user.email_user(subject, message)
        toemail = form.cleaned_data.get('email')
        email = EmailMessage(subject, message, to=[toemail])
        email.send()
        return HttpResponse('Please confirm your email address to complete the registration')
else:
    form = SignupForm()
return render(request, 'signup.html', {'form': form})

And this is a part of the template i did :

<div class="centre">
  <h1>Register For An Account To Access Ultra Marker API</h1>
  <form class="form" method="post">
  {% csrf_token %}
     <input type="text" class="name" placeholder="First Name", value={{  }} >
     <input type="text" class="name" placeholder="Last Name", value={{  }} >
     <input type="email" class="email" placeholder="Email", value={{  }} >
     <input type="text" class="name" placeholder="Username", value={{  }} >
     <input type="password"required autocomplete="off" placeholder="Password", value={{  }} />
     <input type="password"required autocomplete="off" placeholder="Confirm Password" value={{  }} />
     <input type="submit" class="submit" value="Register">
     <br/>
     <br/>
     <div style="text-align: right;padding-right: 10px">
     <a href="login/">Already registred ? Log in !</a>
  </div>
</form>

My forms.py:

class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

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

Upvotes: 0

Views: 195

Answers (1)

hansTheFranz
hansTheFranz

Reputation: 2580

Ok so first thing here is ALWAYS check if if form.is_valid(): is true at some point. Easiest way to check that is print test and check the console when you hit submit button for the form.

in your views.py file you set up in the last line return render(request, 'signup.html', {'form': form}) with this you are passing the keyword form into the template and you can use it there.

<form method="POST" enctype='multipart/form-data' >
{% csrf_token %}
{{form}}
<button type="submit" class="btn ban-default fa fa-cloud-upload">
</form>

in your template you can use the form keyword like this. now you form should show up and then can be rendered in the form validation.

EDIT

Since its more about how to style a form: In my templates I use Widget Tweaks

<form method='POST' action="/" enctype='multipart/form-data'>
 {% load widget_tweaks %}
 {% csrf_token %}
 {{ form.first_name |add_class:"customCSS1 customCSS2" }}
 {{ form.second_name |add_class:"customCSS3 customCSS4" }}
</form>
{{ form.media.js }}

with this plugin you can style the form as you wish. All Css classes work

Upvotes: 2

Related Questions