Есть модель:
class Novel(models.Model):
slug = models.SlugField()
title_rus = models.CharField(max_length=100)
novel_txt = models.FileField(upload_to='uploads/books_txt', blank=True, null=True)
Представление, которое читает txt-файл, и возвращает содержание txt, а также содержание поля title_rus из модели
def novel_text(request, slug):
get_file = Novel.objects.get(slug=slug).novel_txt
file_path = os.path.join('media', str(get_file))
data_file = open(file_path, 'r')
text = data_file.read()
novels = Novel.objects.get(slug=slug).title_rus
context = {
'text': text,
'novels': novels
}
return render(request, 'novels/text.html', context)
Шаблон
{% extends 'novels/base.html' %}
{% load staticfiles %}
{% block content %}
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<h1 class="display-4">{{ novels }}</h1>
</div>
<div class="container">
<div>{{ text|linebreaksbr }}</div>
<div>
{% endblock %}
Все работает, но возникает ошибка:
Traceback (most recent call last):
File "/home/apheyhys/PycharmProjects/jhchase/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/apheyhys/PycharmProjects/jhchase/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/apheyhys/PycharmProjects/jhchase/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/apheyhys/PycharmProjects/jhchase/novels/views.py", line 15, in novel_text
get_file = Novel.objects.get(slug=slug).novel_txt
File "/home/apheyhys/PycharmProjects/jhchase/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/apheyhys/PycharmProjects/jhchase/venv/lib/python3.7/site-packages/django/db/models/query.py", line 399, in get
self.model._meta.object_name
novels.models.Novel.DoesNotExist: Novel matching query does not exist.