0
0 комментариев
import os
from flask import Flask, render_template, redirect, g, request, url_for, abort, send_from_directory
 
 
app = Flask(__name__)
 
 
supported_languages = ["en", "ru"]
 
 
@app.before_request
def before():
    if request.view_args and 'lang_code' in request.view_args:
        if request.view_args['lang_code'] not in supported_languages:
            return abort(404)
        g.current_lang = request.view_args['lang_code']
    else:
        if request.accept_languages.best_match(supported_languages):
            request.view_args['lang_code'] = request.accept_languages.best_match(supported_languages)
        else:
            request.view_args['lang_code'] = 'en'
        g.current_lang = request.view_args['lang_code']
    if request.endpoint == 'favicon_index':
        request.view_args.pop('lang_code')
 
 
@app.route('/favicon.ico')
def favicon_index():
    return send_from_directory(os.path.join(app.root_path, 'static'),
                               'favicon.ico', mimetype='image/vnd.microsoft.icon')
 
 
@app.route('/')
def root(lang_code):
    return redirect(url_for('index', lang_code=lang_code))
 
 
@app.route('/<lang_code>')
def index(lang_code):
    return render_template("/"+lang_code+"/index.html")
 
 
@app.route('/<lang_code>/about')
def about(lang_code):
    return render_template("/"+lang_code+"/about.html")
 
 
if __name__ == '__main__':
    app.run(debug=True)

Делаю мультиязычный сайт.

Вывод консоли:

127.0.0.1 - - [12/Mar/2018 15:13:16] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [12/Mar/2018 15:13:16] "GET /ru HTTP/1.1" 200 -
127.0.0.1 - - [12/Mar/2018 15:13:17] "GET /static/favicon.ico HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/ash/CODE/Flask/multilang01/env/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: send_static_file() got an unexpected keyword argument 'lang_code'

Получается когда он хочет вернуть favicon.ico там срабатывает функция send_static_file() которая не настроена на получение параметра ‘lang_code’.
Я новичок и хз как это все внутри работает, облазил весь инет и не нашел решения. Помогите пожалуйста.

Пробовал код before() обернуть конструкцией

if request.endpoint != 'favicon_index':
    if request.view_args and 'lang_code' in request.view_args:
        if request.view_args['lang_code'] not in supported_languages:
            return abort(404)
        g.current_lang = request.view_args['lang_code']
    else:
        if request.accept_languages.best_match(supported_languages):
            request.view_args['lang_code'] = request.accept_languages.best_match(supported_languages)
        else:
            request.view_args['lang_code'] = 'en'
        g.current_lang = request.view_args['lang_code']

Не помогло.


Добавить комментарий