简体   繁体   English

Django:将上下文发送到“ base.html”

[英]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'. 因为它将在不同的页面中,所以我想在“ 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). 我发现了有关“上下文处理器”( Django-如何使变量可用于所有模板? )的信息,但是,正如我所看到的,它仅将变量传递给函数中的所有模板( 使用查看类,以及get和post方法。

For that, it uses a function like this one on the file: 为此,它在文件上使用了类似这样的功能:

view.py : 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'. 但是,我在views.py中使用了类视图,并且通常,我确实传递了“请求”,“要渲染的URL”,“上下文”。 Like this: 像这样:

view.py : 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? 我的问题是在View中返回什么?

After following the steps for seetting up the 'context-processor.py', in this file, I have: 在遵循了在文件中插入“ context-processor.py”的步骤之后,我得到了:

path: app_cliente/context-processor.py 路径: app_cliente / context-processor.py

File: context-processor.py : 文件: 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? 在我的urls.py中,此视图应从哪个URL调用?

Is this the correct approach? 这是正确的方法吗?

UPDATE 1: 更新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??? 在这里, RegistroClienteView.as_view()将呈现哪个模板??? Remember that it only returns a context in the context_processor.py file. 请记住,它仅在context_processor.py文件中返回上下文。

You should replace return context with return render(request, 'PATH/TEMPLATE.html', context) . 您应该将return context替换为return render(request, 'PATH/TEMPLATE.html', context)

This also resolves your question which template it renders :-) 这也解决了您的问题,它呈现哪个模板:-)

To have a navbar template in your base.html, create templates/myapp/navbar.html and then in your 要在base.html中具有导航栏模板,请先创建templates/myapp/navbar.html ,然后在

base.html base.html

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

If you want to show the user name you can add in your navbar.html 如果要显示用户名,可以在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: 在视图中使用TemplateView定义要使用的模板:

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. 您可以设置所需的任何URL,只要您通过视图即可。 as in

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM