Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Terms and Conditions form

I'm trying to figure out the most elegant way to build a terms and conditions (TNC)form using django. The user has to agree to the TNC in order to continue. The confusing part is how embed a scrolling text field into the form with the TNC that is not editable. Then the user has to click the check box or the form is invalid. The TNC is a substantial doc and is located in a text file. Is there a way to load the text file and make that the content of the scrolling field.

Any examples of this type of form or something similar?

Thanks

like image 591
codingJoe Avatar asked Jun 03 '12 05:06

codingJoe


People also ask

How do I make a valid form in Django?

Django provides built-in methods to validate form data automatically. Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

What is Forloop counter in Django?

A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.

What is escape in Django?

Definition and Usage. The escape filter escapes HTML characters from the value. Note: Escaping HTML characters is a default setting in Django, so we have to turn off autoescape in the example to be able to see the difference.


1 Answers

The confusing part is how embed a scrolling text field into the form with the TNC that is not editable.

This confusing part is easy: it's not a form element. It's just text.

Get your content somehow... say from a file as you suggest:

context = {}
with open('/terms-and-conditions.txt') as f:
    context['terms'] = f.read()

Define a simple form:

class MyForm(forms.Form):
    i_agree = forms.BooleanField()

Pass both to your template...

<div style="width:600px; height:300px; overflow-y:scroll;">
    {% if form.errors %}
        <h1>You must agree to the TNC</h1>
    {% endif %}
    <form method="post">
        {{ form.as_p }}
        <input type="submit" value="I agree to the TNC" />
    </form>
</div>

Anything else is just a permutation of this simple pattern. Perhaps you use readonly textarea, a javascript warning, etc.

like image 134
Yuji 'Tomita' Tomita Avatar answered Oct 10 '22 06:10

Yuji 'Tomita' Tomita