Utworzyłem plik template.html z ramki danych pandy, który zapisałem do renderowania w moim pliku HTML.

 df1 = series.to_frame()
 df2 = series1.to_frame()
 table1 = df1.to_html()
 table2 = df2.to_html()

Tak więc przekazuję ten parametr dla funkcji create_file_html i zwracam żądanie przekazania funkcji widoku.

create_file_html(table1, table2)
return result(request)

Moja funkcja tworzy 2 pliki html

def create_file_html(table1, table2):
    with open('templates/table1.html', 'w') as t1:
        t1.write(table1)
    with open('templates/table2.html', 'w') as t2:
        t2.write(table2)

Więc zwracam żądanie wywołania funkcji widoku, aby zmiękczyć mój kod HTML.

def result(request):
    return render(request, 'result.html')

W moim pliku result.html wstawiam blok kodu:

{% extends 'base.html' %}

{% block 'content' %}
<h1 align="center">Análise</h1>
<hr class="my-4">
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <table>
                {% include 'table1.html' %}
                {% include 'table2.html' %}
            </table>
        <hr class="my-4">
        <div class="form-row">
            <div class="col-7">
                <a class="btn btn-danger" href="{% url 'home' %}">Return</a>
            </div>
        </div>
    </div>
</div>
{% endblock %}

Tak więc, gdy próbuję go wyrenderować, pojawia się błąd:

File "c:\users\eric2\appdata\local\programs\python\python37\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 347: invalid continuation byte

Kompletny wynik to:

Internal Server Error: /analise
Traceback (most recent call last):
  File "D:\Projetos Dev\.venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Projetos Dev\analise\views.py", line 100, in analise
    return result(request)
  File "D:\Projetos Dev\analise\views.py", line 23, in result
    return render(request, 'result.html')
  File "D:\Projetos Dev\.venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\loader_tags.py", line 176, in render
    template = context.template.engine.get_template(template_name)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\loaders\base.py", line 24, in get_template
    contents = self.get_contents(origin)
  File "D:\Projetos Dev\.venv\lib\site-packages\django\template\loaders\filesystem.py", line 24, in get_contents
    return fp.read()
  File "c:\users\eric2\appdata\local\programs\python\python37\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 347: invalid continuation byte

Jak mogę to rozwiązać?

-1
Eric Gomes 23 marzec 2020, 06:16

2 odpowiedzi

Najlepsza odpowiedź

@Eric Gomes, to powinien być komentarz, ale wymaga zbyt wiele pisania. Jesteś tu naprawdę zagubiony. Powinieneś poświęcić więcej czasu na przeczytanie dokumentacji.

{% extends 'base.html' %}

 {% block 'content' %}
 <h1 align="center">Análise</h1>
<hr class="my-4">
<div class="container-fluid">
   <div class="row justify-content-center">
      <div class="col-md-8">
          <table>
             {{ table_1 | safe }}
            {{ table_2 | safe }}
        </table>
    <hr class="my-4">
    <div class="form-row">
        <div class="col-7">
            <a class="btn btn-danger" href="{% url 'home' %}">Return</a>
        </div>
    </div>
</div>

{% endblock%}

Następnie określ w ten sposób swój pogląd

def my_view(request, *args, **kwargs):
     my_table_1 = get_my_pandas_table(request, **kwargs)
     my_table_2 = get_my_pandas_table_2(request, **kwargs)
     context = {'table_1': my_table_1, 'table_2': my_table_2}
     return render(request, 'my_template.html', context)

W ten sposób tabela_1 i tabela_2 są dostępne w zmiennych szablonu i mogą być renderowane. Tag safe jest wymagany, aby włączyć renderowanie html. Jest to funkcja bezpieczeństwa w django. Poświęć trochę czasu na przeczytanie dokumentacji.

0
unlockme 23 marzec 2020, 04:11

Spróbuj dodać ten ".decode ('utf-8', 'ignore'). Encode (" utf-8 ")" do wartości usuwania bajtów innych niż utf-8.

def create_file_html(table1, table2):
    with open('templates/table1.html', 'w') as t1:
        t1.write(table1.decode('utf-8','ignore').encode("utf-8"))
    with open('templates/table2.html', 'w') as t2:
        t2.write(table2.decode('utf-8','ignore').encode("utf-8"))
0
django11 23 marzec 2020, 04:11