A
A
apheyhys2018-10-18 16:09:01
Django
apheyhys, 2018-10-18 16:09:01

Why does the matching query does not exist error occur?

There is a model:

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)

A view that reads a txt file and returns the content of the txt as well as the content of the title_rus field from the model
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)

Sample
{% 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 %}

Everything works, but an error occurs:
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.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2018-10-18
@netpastor

get_file = Novel.objects.get(slug=slug).novel_txt
...
novels.models.Novel.DoesNotExist: Novel matching query does not exist.

There is no object in the database with this slug

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question