Django:将上下文发送到"base.html" [英] Django: send context to 'base.html'

查看:66
本文介绍了Django:将上下文发送到"base.html"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于注册/登录的导航栏项目.因为它将在不同的页面中,所以我正在考虑在"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}

但是,我在views.py中使用了类视图,并且通常,我确实传递了请求",要渲染的URL",上下文".像这样:

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?

按照该文件中的'context-processor.py'步骤填充后,我具有:

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

路径: app_cliente/context-processor.py

文件: context-processor.py :

请注意,我只是返回上下文

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

问题:

在我的urls.py中,此视图应从哪个URL调用?

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

这是正确的方法吗?

更新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'),

在这里, RegistroClienteView.as_view()将呈现哪个模板???请记住,它仅在 context_processor.py 文件中返回上下文.

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

推荐答案

您应将 return context 替换为 return render(request,'PATH/TEMPLATE.html',context)代码>.

这也解决了您的问题,它呈现的模板是:-)

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

这篇关于Django:将上下文发送到"base.html"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆