Omar Gonzales
Omar Gonzales

Reputation: 4008

Django: send context to 'base.html'

I have a nav bar item that is used to register/login. As, it's gonna be in different pages, I'm thinking of calling this form "once" for every page, in the 'base.html'.

I've found about the 'context processor' (Django - How to make a variable available to all templates?), however, as I can see, it passess only a variable to all templates, in a function (Doesn't use a View class, with get and post methods).

For that, it uses a function like this one on the file:

view.py:

def categories_processor(request):
    categories = Category.objects.all()            
    return {'categories': categories}

However, I'm using Class Views in my views.py, and, generally, I do pass the 'request', 'url to be rendered', 'context'. Like this:

view.py:

class RegistroClienteView(View):
    def get(self, request):
        ls_tipos_de_documento = TipoDocumento.objects.values_list('id', 'nombre_corto')
        form = ClienteCreationForm()
        context = {'form': form, 'ls_tipos_de_documento': ls_tipos_de_documento}
        return render(request, 'app_cliente/frontend/ingreso.html', context)

    def post(self, request):
        ls_tipos_de_documento = TipoDocumento.objects.values_list('id', 'nombre_corto')
        form = ClienteCreationForm(request.POST)
        if form.is_valid():
            form.save()
            context = {'form': form}
            return render(request, 'app_cliente/frontend/ingreso.html', context)

        context = {'form': form, 'ls_tipos_de_documento': ls_tipos_de_documento}
        return render(request, 'app_cliente/frontend/ingreso.html', context)

My question is what to return in the View?

After following the steps for seetting up the 'context-processor.py', in this file, I have:

path: app_cliente/context-processor.py

File: context-processor.py:

Please, notice that I'm only returning the context

from app_cliente.forms import ClienteCreationForm
from django.views import View
from nucleo.models.tipo_documento import TipoDocumento


class RegistroClienteView(View):
    def get(self, request):
        ls_tipos_de_documento = TipoDocumento.objects.values_list('id', 'nombre_corto')
        form = ClienteCreationForm()
        context = {'form': form, 'ls_tipos_de_documento': ls_tipos_de_documento}
        return context

    def post(self, request):
        ls_tipos_de_documento = TipoDocumento.objects.values_list('id', 'nombre_corto')
        form = ClienteCreationForm(request.POST)
        if form.is_valid():
            form.save()
            context = {'form': form}
            return context

        context = {'form': form, 'ls_tipos_de_documento': ls_tipos_de_documento}
        return context

Question:

In my urls.py, in which url should this View called from?

Is this the correct approach?

UPDATE 1:

Let me clarify my question using this example posted in the comments:

url(r'^any-url-i-want/$', RegistroClienteView.as_view(), name='any-name-i-want'),

Here, RegistroClienteView.as_view() would render which template??? Remember that it only returns a context in the context_processor.py file.

Upvotes: 0

Views: 7182

Answers (2)

schrodingerscatcuriosity
schrodingerscatcuriosity

Reputation: 1860

To have a navbar template in your base.html, create templates/myapp/navbar.html and then in your

base.html

...
include('myapp/navbar.html')
...

If you want to show the user name you can add in your navbar.html

{% if request.user.is_authenticated %}
{{ request.user.username }}
{% endif %}

If you want to add other data to your navbar, use context processors.

Use TemplateView in your view to define wich template to use:

from django.views.generic import TemplateView

class RegistroClienteView(TemplateView):
    template_name = 'myapp/my_template.html'
    ...

Finally as stated in my comment:

You can set any url you want, as long as you pass the view. as in

url(r'^any-url-i-want/$', RegistroClienteView.as_view(), name='any-name-i-want'),

Upvotes: 0

porton
porton

Reputation: 5801

You should replace return context with return render(request, 'PATH/TEMPLATE.html', context).

This also resolves your question which template it renders :-)

Upvotes: 2

Related Questions