Issue trying to change language from Django template

前端 未结 7 2160
悲&欢浪女
悲&欢浪女 2020-12-01 06:46

I need to include two buttons or links to allow users change language between English and Spanish. I\'ve read the docs and tried this:

7条回答
  •  情话喂你
    2020-12-01 07:32

    Besides adding form that was suggested here:

    
      {% csrf_token %}
      {{ request.get_full_path_info|slice:'3:'}}
      
      
    
    

    I would suggest adding a context processor (app.context_processors.py):

    def language_processor(request):
    """
    This is needed for language picker to work
    """
    return {
        'languageless_url':
        '/' + '/'.join(request.get_full_path().split('/')[2:])
    }
    

    This allows to leave the logic out of template. Also don't forget to add your processor in template settings:

                'context_processors': [
                'app.context_processors.language_processor',
    

提交回复
热议问题