R
R
rm root2020-06-10 16:48:16
Django
rm root, 2020-06-10 16:48:16

Django 3, need help registering template tags, how to implement it?

Here is an article but it gives me an error

i know that it can be done through a view, please don't write about it, i'm interested in this way

the task is
i need to create a function that returns from the class (name)
and when i add {% load example %} in the template i could write {{ example_method }} and the function would return a string from the name field,

I created it as in the templatetags/poll_extrax.py article.
Applications in settings are registered.
5ee0e2f72ecdd680922858.png

in template {% load poll_extras %}
code in poll_extrax.py

from django import template
from ..models import NameCinema

register = template.Library()

@register.simple_tag
def namecinema():
    value = NameCinema.objects.all()[0].name
    return value


console error
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\urls\resolvers.py", line 590, in url_patterns
    iter(patterns)
TypeError: 'module' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "c:\python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\core\management\base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\reabk\Desktop\aaa\cinemax-django\venv\lib\site-packages\django\urls\resolvers.py", line 597, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'cinemax_project1.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by 
a circular import.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-12
@pyHammer

@register.simple_tag
def namecinema():
    name_cinema = NameCinema.objects.first()
    if name_cinema:
        return name_cinema.name
    return ''

Try rewriting the tag like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question