0

I have a number of forms for a website which have been prewritten. I am required to keep these as they are and not modify them.

I am also required to move these to a Django 2.1 back end, which appears to have a built in form generation system, which doesn't appear to be compatible with the forms in themselves (I.E. The Jinja Scripting for forms appears to auto generate the forms without having any real choice in the matter).

I cannot find any documentation on how to move pre existing forms into Django, and I have no idea how.

Thanks for helping me.

    <div class="container">
    <form class="form-signin" name="login" action="timesheet">
        <h1 class="h3 mb-3 font-weight-normal">Sign In</h1>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
        <div class="checkbox mb-3">
            <label>
                <input type="checkbox" value="remember-me"> Remember me
            </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    </form>
</div>

1 Answers1

0

You are right Django has its own way how it handles forms but luckily it is fully customizable. Let's say you have a LoginForm that you want to show to the user:

you will have to create a Form in Django that represents the field. Additionnaly since, the form will authenticate the user in your django system if the credentials are correct you will have make this form inherit from Django's built in AuthentificationForm: forms.py:

from django.contrib.auth.forms import AuthenticationForm
class EmailAuthenticationForm(AuthenticationForm):
    required_css_class = 'required'
    remember_me = forms.BooleanField(
        required=False,
        label='Remember me for two weeks',
    )

    def clean_username(self):
        """Prevent case-sensitive erros in email/username."""
        return self.cleaned_data['username'].lower()

Next we have to serve the form to the user (through html). Therefore we create a view that renders the form in the views.py of your app: views.py:

from forms import EmailAuthentificationForm
def login(request):
    if request.method == 'POST':
        form = EmailAuthenticationForm(request.POST)
        if form.is_valid():
            pass  # does nothing, just trigger the validation
    else:
        form = EmailAuthenticationForm()
    return render(request, 'login.html', {'form': form})

now to make our life easier django can render the form automatically for you without any styling just to make things work. But since we have our own html layout with our own styles what we want to do is render the form manually: login.html

<div class="container">
<form method="post" class="form-signin" name="login" action="timesheet" novalidate>
    {% csrf_token %}


    {{ form.non_field_errors }}

    {{ form.source.errors }}
    {{ form.source }}

    <h1 class="h3 mb-3 font-weight-normal">Sign In</h1>
    <label for="inputEmail" class="sr-only">Email address</label>
    {{form.email}} 
    <label for="inputPassword" class="sr-only">Password</label>
   {{form.password}}
    <div class="checkbox mb-3">
        <label>
           {{form.remember_me}}
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

now the email and password field will still not be rendered with all the css attributes that you would like the fields to have in order to do this you need add addittional attributes to your email and password fields in the Authentification form. To find out how to do that read more here To read more about rendering your own custom form in django read here

matyas
  • 2,696
  • 23
  • 29