2017-03-02 3 views
0

Ich bin ein neues Projekt in TDD Idee beginnen, also zuerst schrieb ich Tests. Aber ich habe ein Problem mit utf (glaube ich)Django und Python 2.7 Problem mit Unicode in Form Fehler

Ich habe einfaches Login-Formular

from django.utils.translation import ugettext_lazy as _ 

ERRORS_MESSAGES = { 
    'not_active': _(u"Użytkownik nie jest aktywny"), 
    'user_not_exist': _(u"Użytkownik o podanym loginie już istnieje") 
} 


class LoginForm(forms.Form): 
    username = forms.CharField(max_length=50) 
    password = forms.CharField(max_length=50, widget=forms.PasswordInput) 

    def clean(self): 
     username = self.cleaned_data.get('username') 
     password = self.cleaned_data.get('password') 
     user = authenticate(username=username, password=password) 
     if not user: 
      raise forms.ValidationError(ERRORS_MESSAGES['user_not_exist']) 
     if not user.is_active: 
      raise forms.ValidationError(ERRORS_MESSAGES['not_active']) 
     return self.cleaned_data 

Und in Test:

user = User.objects.create(
    username='asd', password='asd', email='[email protected]' 
) 
form = LoginForm(data={'username': 'asd', 'password': 'asd'}) 
self.assertEqual(form.is_valid(), False) 
print "ERRORS:", form.errors['__all__'] 
self.assertIn(ERRORS_MESSAGES['not_active'], form.errors) 

Und Konsolenausgabe:

# here, char 'ż' looks fine, 
ERRORS: <ul class="errorlist nonfield"><li>Użytkownik o podanym loginie już istnieje</li></ul> 
# but next: 
self.assertIn(ERRORS_MESSAGES['not_active'], form.errors) 

AssertionError : nicht gefunden in {'alle': [u'u \ u017cytkownik o podanym loginie ju \ u017c istnieje ']}

Antwort

0

hast du

#-*- encoding: utf-8 -*- 

auf der ersten Zeile der Datei?

EDIT: Von Nachricht kann ich annehmen, dass es bereits einen Benutzer mit asd Namen gibt.

+0

Ja ich habe # - * - encoding: utf-8 - * - – Jeroj82