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.