开发者

Django Form Errors not showing

I have a form validator and when it fails, the error message doe开发者_如何学Pythonsn'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>


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜