【问题标题】:How to Render contact-from in every view (Django)如何在每个视图中呈现联系人(Django)
【发布时间】:2020-11-15 13:47:41
【问题描述】:

联系表格被放置在页脚。而且大多数不同的视图都有页脚部分。

def about-page(request):
return render(request, 'about.html')

def faq-page(request):
    return render(request, 'faq.html')

def home-page(request):
    return render(request, 'home.html')

以上所有模板都有“包含”页脚部分:

{% include 'footer.html' %}

这还包括联系表格:

{% include 'contact.html' %}

所以,现在如果我想在所有页面上呈现联系表单,我必须在上下文中传递它。喜欢:

def about-page(request):
form = ContactForm()
return render(request, 'about.html',{'form':form})

def faq-page(request):
form = ContactForm()
return render(request, 'faq.html',{'form':form})

def home-page(request):
form = ContactForm()
return render(request, 'homeabout.html',{'form':form})

是否有任何通用功能可以通过不重复向所有页面添加表单。因为我可以访问整个网站的许多页面。

【问题讨论】:

    标签: django django-forms django-views


    【解决方案1】:

    如果它是一个函数,我看不到任何扩展方式。您是否考虑过切换到基于类的视图?这样,您可以执行以下操作:

    class BaseView(FormView):
        form = ContactForm()
    
    class HomePage(BaseView):
        template_name = 'home.html'
    
    class FAQPage(BaseView):
        template_name = 'faq.html'
    

    在这种情况下,BaseView 将是包含表单的父视图,您可以在所有子类中使用它。 BaseView 继承的 FormView 是 Django 用于呈现表单的默认通用视图之一。

    提示:在 URL 中使用基于类的视图时,记得添加 .as_view() 函数:

    urlpatterns = [
        path('', views.HomePage.as_view(), name='home'),
    ]
    

    【讨论】:

    • 我知道我可以使用 CBV。但我在整个项目中都使用了 FBV,我不想失去规律性和一致性。这就是为什么,我想知道我是否可以用 FBV 做些什么
    【解决方案2】:

    如果你想坚持功能,使用部分:

    from functools import partial
    
    def template_with_contact_form(template, request):
        return render(request, template, {"form": ContactForm()})
    
    
    about = partial(template_with_contact_form, "about.html")
    

    【讨论】:

      【解决方案3】:

      您可以使用class based views 来完成此操作:

      from django.views import View
      
      class BaseView(TemplateView):
          def get_context_data(self, **kwargs):
              context = super().get_context_data(**kwargs)
              context['contact_form'] = ContactForm()
              return context
      

      现在,在继承自 BaseView 的其他视图中,contact_form 将自动添加到上下文中。

      class AboutView(BaseView):
          template_name = 'about.html'
      
      class FaqView(BaseView):
          template_name = 'faq.html'
      
      class HomeView(BaseView):
          template_name = 'home.html'
      

      如果您真的想使用基于函数的视图,您可以编写自己的上下文处理器 (docs)。

      在文件myapp/context.py

      def my_context_processor(request):
          return {'contact_form': ContactForm()}
      

      现在在您的settings.py 中,将上下文处理器添加到TEMPLATES 选项列表中,如下所示:

      TEMPLATES = [
          {
              'OPTIONS': {
                  'context_processors': [
                      'django.template.context_processors.debug',
                      'django.template.context_processors.request',
                      'django.contrib.auth.context_processors.auth',
                      'django.contrib.messages.context_processors.messages',
      
                      'myapp.context.my_context_processor'
                  ],
              },
          },
      ]
      

      这种方法的唯一缺点是contact_form 将被插入到每个 视图的上下文中,这在您的情况下可能无关紧要。

      【讨论】:

      • 您的回答也很好并且有效,但我在整个项目中都使用了 FBV,我不想失去规律性和一致性。这就是为什么,我想知道是否有什么我可以用 FBV 做的任何建议?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 2012-10-22
      相关资源
      最近更新 更多