0

I have a form validator and when it fails, the error message doesn't show. Any idea what I'm missing?

views.py:

def setup_onesheet(request):
    # if the form has been submitted
    if request.method == 'POST':
        if 'unverified_username' in request.POST:
            form = OnesheetURL(request.POST)
            if form.is_valid():
                ...

    form = OnesheetURL()
    variables = RequestContext(request, {
        'error_message': error_message,
        'form' : form,
    });
    return render_to_response('onesheet_setup/setup_new_onesheet.html', variables)

forms.py:

class OnesheetURL(forms.Form):
    unverified_username = forms.CharField(label='http://onesheet.com/', max_length=75, validators=[validate_slug])

template:

<form method="post" action="/setup/new/" id="verify-identity" class="full">
    {% csrf_token %}
    <ul>
        <li>
            {{ form.unverified_username.errors }}
            {{ form.unverified_username.label_tag }}
            {{ form.unverified_username }}
            <p>Onesheet URL can only contain letters, numbers, underscores ( _ ) or hyphens ( - ).</p>
        </li>
    </ul>
    <input type="submit">
</form>

1 Answer 1

1

Try:

def setup_onesheet(request):
    # if the form has been submitted
    if request.method == 'POST':
        if 'unverified_username' in request.POST:
            form = OnesheetURL(request.POST)
            if form.is_valid():
                ...
    else:
        form = OnesheetURL()

    variables = RequestContext(request, {
        'error_message': error_message,
        'form' : form,
    });
    return render_to_response('onesheet_setup/setup_new_onesheet.html', variables)
Sign up to request clarification or add additional context in comments.

1 Comment

Dude - no worries. I facepalm on a regular basis :)

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.