Maxsteel
Maxsteel

Reputation: 2040

Use pure HTML page for registration in Django

I am a Django newbie, so please go easy on me. I have the entire front-end of the website ready for use. I am writing back-end in Django. So, my homepage has a registration form and I want to register user using Django-registration-redux. What I don't understand is, is it necessary to use Django form to generate form and or I can simply use plain html with post data going to Django backend. The form looks like this:

<form role="form" id="user_form" method="post"
action="/appname/register" enctype="multipart/form-data">
    {% csrf_token %}
    {% if registered %}
        <strong>Thank you for registering! Check your email and confirm registration.
       </strong>
    {% else %}
        <div class="form-group">
            <label for="register-username" style="color: rgba(255, 255, 0, 0.68);font-weight: 500;">
                <i class="icon-user"></i>
                <b>Email</b>
            </label>
            <input class="form-control" id="register-username" type="text" placeholder="" required>
        </div>
        <div class="form-group" style="color: rgba(255, 255, 0, 0.68);font-weight: 500;">
            <label for="register-password">
                <i class="icon-lock"></i>
                <b>Password</b>
            </label>
            <input class="form-control" id="register-password" type="password" placeholder="" required>
        </div>
</form>

Upvotes: 2

Views: 1856

Answers (5)

a134man
a134man

Reputation: 150

i guess you haven't mentioned the name attribute in input fields. If you don't want to use django form functionality, its all right. But remember to provide the name attributes in your form and they should have values equal to those defined in the model. For example, let's say my model is class Post(models.Model): title = models.CharField(max_length=100) Then, in your form, you should declare an input field like this:

Upvotes: 2

Andy
Andy

Reputation: 76

There are several ways to use plain HTMLs in Django forms. In fact I think that is the most cases since the form system Django provides is simply not flexible enough.

  1. You can try accessing the POST parameters directly. e.g.

    views.py:

    ...
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    register(username, password)
    ...
    

    template.html

    <form action="/path" method="post">
      <input type="text" name="username"/>
      <input type="password" name="password"/>
    </form>
    

    Note that you may have to deal with CSRF tokens.

  2. If you already have a Django form, e.g.

    class LoginForm(forms.Form):
        email = forms.EmailField(label="Email")
        password = forms.CharField(label="Password", max_length=255)
    

    you can just match the property names (e.g. email) with the name attribute in HTML forms. e.g. template.html

    <form action="/path" method="post">
      <input type="email" name="email"/>
      <input type="password" name="password"/>
    </form>
    

    then in your views.py, you can instantiate the form and access data in it with

    login_form = LoginForm(request.POST)
    email = login_form.cleaned_data['email']
    password = login_form.cleaned_data['password']
    login(email, password)
    

Upvotes: 4

Daniel Roseman
Daniel Roseman

Reputation: 599610

You certainly can use pure HTML, as long as you give your input elements the name attributes that Django is expecting (you don't seem to have given them any names at all, so the form wouldn't even submit any data).

However, by doing this you are missing out on the ability to re-display the form with errors and partially-filled data when it fails to validate correctly. Since the only customization your fields appear to have is the addition of a "form-control" class, you might as well just make that change in the Django form.

Upvotes: 1

Wtower
Wtower

Reputation: 19902

You do not have to use Django forms whatsoever. You can handle the post variables at will in your views.py:

if request.method == 'POST':
    # do something with request.POST

Nevertheless, I would recommend you to take advantage of the functionality that Django forms offer.

Upvotes: 3

rnevius
rnevius

Reputation: 27092

Yes, you can always use a plain HTML form, as long as the input names match what will be validated by your registration app.

Upvotes: 0

Related Questions