2013-09-23 3 views
0

이렇게 보이는 양식이 있는데 일부 필드를 비워두면 오류 사전에 올바른 오류 메시지가 표시되기 때문에 이메일이 올바르게 유효성 검사를하지 않는 것 같습니다.Django 양식이 부적절한 이메일에 대해 ValidationError를 발생시키지 않습니다.

forms.py

class NewUserForm(Form): 
    username = UsernameField(required=True) 
    password = CharField(required=True, widget=PasswordInput()) 
    email = EmailField(required=True) 

그리고 views.py에 내가 가진

form = NewUserForm(request.POST) 
    if form.is_valid(): 
     # Do stuff 
에서 내가 이메일로 같은 은 가지고 있지 실패한다 "@"(A ValidationError의 원인이해야하는)를 입력하면

https://github.com/django/django/blob/master/django/core/validators.py (라인 105)

I에 명시된 바와 같이

슬로우 내가 뭘 잘못하고 있는지 모르겠다. 사실, 전자 메일을 입력하지 않아도 필드가 필요 없다고 말하는 것조차 없다.

+0

템플릿에 양식을 어떻게 표시하고 있습니까? 'request.POST'에 포함 된 것을 확인하기 위해 print 또는 log 문을 추가하십시오. – Alasdair

답변

0

폼 개체는 유효성 검사 오류를 시도/제외합니다. 당신은 오류가있는 폼을 표시하려는 경우, 당신은 할했습니다 :

form = NewUserForm(request.POST) 
context = {} 
if form.is_valid(): 
    # Do stuff 
else: 
    # "is_valid()" fills the errors and non_field_errors attributes of the form, you can render it now 
    context['myform'] = form 
    render_to_response('mytemplate.html', context, context_instance = RequestContext(request)) 

그리고 템플릿에서

, {{ form }}, {{ form.as_p }}, {{ form.as_ul }}는 빨간색의 오류가있는 양식을 렌더링합니다.

관련 문제