Programing

자체 템플릿으로 기본 제공 암호 재설정 / 변경보기를 사용하는 방법

lottogame 2020. 9. 3. 23:39
반응형

자체 템플릿으로 기본 제공 암호 재설정 / 변경보기를 사용하는 방법


예를 들어 컨텍스트에서 내 템플릿 파일 이름으로 url '^/accounts/password/reset/$'가리킬 수 django.contrib.auth.views.password_reset있지만 더 많은 컨텍스트 세부 정보를 보내야한다고 생각합니다.

각 암호 재설정 및보기 변경에 대해 추가 할 컨텍스트를 정확히 알아야합니다.


당신이 소스에서 살펴 경우 django.contrib.auth.views.password_reset 당신은 그것을 사용하는 것을 볼 수 있습니다 RequestContext. 결론은 컨텍스트 프로세서를 사용하여 필요한 정보를 삽입 할 수있는 컨텍스트를 수정할 수 있다는 것입니다.

b-list에는 컨텍스트 프로세서에 대한 좋은 소개가 있습니다.

편집 (실제 질문이 무엇인지 혼란스러워하는 것 같습니다) :

다음과 password_reset같은 명명 된 매개 변수를 사용하는 것을 알 수 있습니다 template_name.

def password_reset(request, is_admin_site=False, 
            template_name='registration/password_reset_form.html',
            email_template_name='registration/password_reset_email.html',
            password_reset_form=PasswordResetForm, 
            token_generator=default_token_generator,
            post_reset_redirect=None):

자세한 내용은 password_reset확인 하십시오.

... 따라서 urls.py는 다음과 같습니다.

from django.conf.urls.defaults import *
from django.contrib.auth.views import password_reset

urlpatterns = patterns('',
     (r'^/accounts/password/reset/$', password_reset, {'template_name': 'my_templates/password_reset.html'}),
     ...
)

django.contrib.auth.views.password_reset'/accounts/password/reset'키워드 인수와 일치하는 URL에 대해 호출됩니다 template_name = 'my_templates/password_reset.html'.

그렇지 않으면 password_reset뷰가 자체적으로 처리 하므로 컨텍스트를 제공 할 필요가 없습니다 . 사용 가능한 컨텍스트를 확인하려면 TemplateSyntax오류를 트리거 하고 스택 추적을 통해라는 로컬 변수가있는 프레임을 찾을 수 context있습니다. 컨텍스트를 수정하려면 위에서 컨텍스트 프로세서에 대해 말한 것이 아마도 갈 길일 것입니다.

요약 : 자체 템플릿을 사용하려면 무엇을해야합니까? template_name뷰가 호출 될 때보 기에 키워드 인수를 제공하십시오 . URL 패턴 튜플의 세 번째 멤버로 사전을 포함하여 뷰에 키워드 인수를 제공 할 수 있습니다.


이 기사를 강력히 추천합니다.

방금 꽂았 고 작동했습니다.

http://garmoncheg.blogspot.com.au/2012/07/django-resetting-passwords-with.html


기존 함수를 래핑하고 원하는 템플릿을 전달하기 만하면됩니다. 예를 들면 :

from django.contrib.auth.views import password_reset

def my_password_reset(request, template_name='path/to/my/template'):
    return password_reset(request, template_name)

이를 보려면 내장 뷰의 함수 선언을 살펴보십시오.

http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py#L74


다음을 수행 할 수 있습니다.

  1. urlpatterns에 추가 (r '^ / accounts / password / reset / $', password_reset)
  2. 템플릿을 '/templates/registration/password_reset_form.html'에 넣으십시오.
  3. INSTALLED_APPS에서 앱이 'django.contrib.auth'앞에 오도록합니다.

설명:

When the templates are loaded, they are searched in your INSTALLED_APPS variable in settings.py . The order is dictated by the definition's rank in INSTALLED_APPS, so since your app come before 'django.contrib.auth' your template were loaded (reference: https://docs.djangoproject.com/en/dev/ref/templates/api/#django.template.loaders.app_directories.Loader).

Motivation of approach:

  1. I want be more dry and don't repeat for any view(defined by django) the template name (they are already defined in django)
  2. I want a smallest url.py

The documentation says that there only one context variable, form.

If you're having trouble with login (which is common), the documentation says there are three context variables:

  • form: A Form object representing the login form. See the forms documentation for more on Form objects.
  • next: The URL to redirect to after successful login. This may contain a query string, too.
  • site_name: The name of the current Site, according to the SITE_ID setting.

I was using this two lines in the url and the template from the admin what i was changing to my need

url(r'^change-password/$', 'django.contrib.auth.views.password_change', {
    'template_name': 'password_change_form.html'}, name="password-change"),
url(r'^change-password-done/$', 'django.contrib.auth.views.password_change_done', {
    'template_name': 'password_change_done.html'
    }, name="password-change-done")

Another, perhaps simpler, solution is to add your override template directory to the DIRS entry of the TEMPLATES setting in settings.py. (I think this setting is new in Django 1.8. It may have been called TEMPLATE_DIRS in previous Django versions.)

Like so:

TEMPLATES = [
   {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # allow overriding templates from other installed apps                                                                                                
        'DIRS': ['my_app/templates'],
        'APP_DIRS': True,
}]

Then put your override template files under my_app/templates. So the overridden password reset template would be my_app/templates/registration/password_reset_form.html

참고URL : https://stackoverflow.com/questions/388800/how-do-i-use-the-built-in-password-reset-change-views-with-my-own-templates

반응형