2011-01-10 6 views
12

이제 기본 로그인을하고 있습니다. 여기에 촬영어떻게 장고 "로그인"폼을 확장합니까?

(r'^login/?$','django.contrib.auth.views.login',{'template_name':'login.html'}), 

:

@csrf_protect 
@never_cache 
def login(request, template_name='registration/login.html', 
      redirect_field_name=REDIRECT_FIELD_NAME, 
      authentication_form=AuthenticationForm): 

가 그보기는 AuthenticationForm 양식 모델을 사용 urls.py에서, 나는있는 contrib 로그인 장고로 이동

class AuthenticationForm(forms.Form): 
    """ 
    Base class for authenticating users. Extend this to get a form that accepts 
    username/password logins. 
    """ 
    username = forms.CharField(label=_("Username"), max_length=30) 
    password = forms.CharField(label=_("Password"), widget=forms.PasswordInput) 

그래서를 .. 내 목표는 사용자 이름 양식을 변경하는 것입니다! 이 번호를 추가하면 widget = forms.TextInput(attrs={'placeholder': 'username'})입니다. 그게 전부 야. 그것이 사용자 이름 입력 상자에 추가하려는 것입니다. 그러나 장고 contrib.py의 일부이기 때문에 실제 django forms.py 파일을 변경하고 싶지는 않으며 파일을 변경하는 것이 좋지 않습니다.

어떻게해야합니까? AuthenticationForm을 확장하는 폼을 생성해야합니까? 그렇다면 가져 오는 방법은 무엇입니까? 그리고 어떻게 내 urls.py 통해 인수로 전달합니까? 나는 무엇을해야할지 모른다.

(r'^login/?$','django.contrib.auth.views.login',{'template_name':'login.html', 'authentication_form':MyAuthenticationForm}), 

나 ',

답변

21

당신은 AuthenticationForm 클래스를 서브 클래 싱해야하고, 당신은 당신 urls.py

class MyAuthenticationForm(AuthenticationForm): 
    # add your form widget here 
    widget = ..... 

그런 다음 전화를하여 urls.py 파일에이 클래스를 가져오고 업데이트 변경해야 당신이 사용해야하는 필드 유형을보기 위해 문서 사이트의 링크를 찾기에는 너무 지쳤습니다. 그러나 장고를 수정하지 않고 시작하려면 트릭을 사용해야합니다. forms.py finitely 해야 변경에 대해 나쁜 느낌!

1

milkypostman으로 말하면 auth.forms.AuthenticationForm의 하위 클래스를 만들어야합니다.

그런 다음 django-crispy-forms를 사용하여 자리 표시자를 원하는 필드에 넣을 수 있습니다. 그것은이만큼 간단합니다

class LoginWithPlaceholder(AuthenticationForm): 

    def __init__(self, *args, **kwargs): 
     super(LoginWithPlaceholder, self).__init__(*args, **kwargs) 
     self.helper = FormHelper() 
     self.helper.form_show_labels = False 
     self.helper.layout = Layout(Div(Field('username', placeholder='username'), css_class="form-group"), 
            Div(Field('password', placeholder='password'), css_class="form-group"), 
            Div(Submit('submit', 'Log in'))) 

마지막으로, 템플릿의 바삭한 태그를 사용하는 것을 잊지 마세요 (응용 프로그램/forms.py) 다음에 대한

<div class="col-sm-6 col-sm-offset-3">   
{% crispy form %}    
</div> 
+0

감사합니다 파삭 파삭 한 포인터! – rikb

관련 문제