0

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.

Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120
  • 1
    In your question you say that you want a navbar element be present in all templates. What does it have todo with context processors? – schrodingerscatcuriosity Dec 19 '17 at 20:01
  • "In my urls.py, in which url should this View called from?" 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'),` – schrodingerscatcuriosity Dec 19 '17 at 20:05
  • @guillermochamorro in the navbar are the login/register forms. As is going to be in different pages, I want to call my view in 'base'.html' once for all. – Omar Gonzales Dec 19 '17 at 20:10
  • @guillermochamorro, please, see the updated question. May you explain that? Ty. – Omar Gonzales Dec 19 '17 at 20:15

2 Answers2

2

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

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

porton
  • 5,214
  • 11
  • 47
  • 95
  • But it's going to be different templates. So, I don't want to hardcode a single 'PATH/TEMPLATE.html'. Is there a way to do it? – Omar Gonzales Dec 19 '17 at 20:38
0

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'),
schrodingerscatcuriosity
  • 1,780
  • 1
  • 16
  • 31
  • But it's going to be different templates. So, I don't want to hardcode a single 'PATH/TEMPLATE.html'. Is there a way to do it? Or the only way is to create a View for every page that loads the navbar with the form? – Omar Gonzales Dec 19 '17 at 20:39
  • @OmarGonzales I realize that you want to create an entire view in your context processor. You don't need to do that. First, context processor take as argument `request`, not `View`. If you have a form template in your base, *the form is rendered on every view*. What you change is the urls your template points to. So in your base.html you have a login form that points to your `url 'login'`, and a register form (remember, the form rendered in every view) that points to `url 'register'.` – schrodingerscatcuriosity Dec 19 '17 at 20:54
  • Your views are available everywhere as long as you have an url pointing at them. Context processors are for having access to specific pieces of data across any template. – schrodingerscatcuriosity Dec 19 '17 at 21:03