Friday, January 28, 2022

Re: Queryset in settings.py?

The solution I think of for this is to overwrite get_template_names

https://ccbv.co.uk/projects/Django/4.0/django.views.generic.base/TemplateView/#get_template_names

I quickly made a draft implementation (which I couldn't test)


from django.core.exceptions import ImproperlyConfigured
from django.views.generic import TemplateView
from app.models import UserTemplate

class PrependTemplatePathMixin:

    def get_template_names(self):
        """
        Return a list of template names to be used for the request. Must return
        a list. May not be called if render_to_response() is overridden.
        """
        if self.template_name is None:
            raise ImproperlyConfigured(
                "PrependTemplatePathMixin requires either a definition of "
                "'template_name' or an implementation of 'get_template_names()'")
        else:
            user_template = UserTemplate.object.get(
                user_id=self.request.user.pk
            )
            template_namespace = user_template.template_namespace.strip().replace('..', '').lstrip('/')
            template_name = f'{template_namespace}/{self.template_name}'
            return [template_name]


class MyTemplateView(PrependTemplatePathMixin, TemplateView):
    template_name = 'my-template.html'



another option is to use or implement something similar to https://github.com/jazzband/django-dbtemplates/


Em sex., 28 de jan. de 2022 às 20:20, Sebastian Jung <sebastian.jung2@gmail.com> escreveu:
Hello Fabio,

i want that admin can make in database a entry to a field in my own settings model in field standardtemplatepath for example /newtemplate/ in my views.py there are exists a normal CBV Templateview with template_name = 'Testtemplate.html' and in settings.py i have a entry TEMPLATES = [
    {
       
        'DIRS': [
            location('templates'),
        ]}

Now when a endcustomer open this view then this view get template from /templates/newtemplate/Testtemplate.html. How can archive this?

Thank you very much

Am Sa., 29. Jan. 2022 um 00:14 Uhr schrieb Fabio C. Barrionuevo da Luz <bnafta@gmail.com>:
Hi Sebastian, I guess this is not possible.

But the real question is: why do you need a queryset to perform in the settings?

Perhaps if you explain what problem you want to solve, we can give you some options for other possible solutions.



Em sex., 28 de jan. de 2022 às 20:00, sebasti...@gmail.com <sebastian.jung2@gmail.com> escreveu:
Hello,

i want to import from a app models.py like this:

from app1.models import classname

then i get:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I want to get a queryset from app1.models.classname in settings.py. How is this possible?

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/18ef496c-c9a1-40df-b251-7ce2cc2dad82n%40googlegroups.com.


--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPVjvMaWUcpwzWePO3XF%2BtqRP3e-YFH4iE9om8ig2hhTeazN%3DQ%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKGT9myirfhUNdMhThSMFwCVStWgpYyFfJUZb6uieD%3Dt%3DMipkA%40mail.gmail.com.


--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPVjvMa-5LXwnHU8UCTdqQYXwvkvFpGc27tYZk8%2BTLDUZfJ1Rw%40mail.gmail.com.

No comments:

Post a Comment