如何在django中的context_processors中包含基于类的视图? [英] how to include class based views in context_processors in django?

查看:138
本文介绍了如何在django中的context_processors中包含基于类的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码在 context_processors.py

  class ContactFormView(FormView):
form_class = ContactForm
template_name =blog / contact.html
success_url =/ contact /
def form_valid(self,form):
contact_name = form.cleaned_data .get('contact_name')
contact_email = form.cleaned_data.get('contact_email')
form_content = form.cleaned_data.get('content','')
try:
send_mail(contact_name,form_content,contact_email,[settings.EMAIL_HOST_USER],fail_silently = False)
除了BadHeaderError:
return HttpResponse('Invalid Header Found')
return super(ContactFormView,self ).form_valid(form)

我想通过使用上下文处理器将其包含在所有的视图中我收到这个错误:

  Type $ at / 
__init __()只需要1个参数(2个给定)
请求方法:GET
请求URL:http://127.0.0.1:8000/
Django版本:1.8.7
异常类型:TypeError
异常值:
__init __()只需要一个参数(2个给定)
异常位置:C:\Python27\lib\site-packages\django-1.8.7-py2.7.egg\django\ bind_template中的template\context.py,第241行
Python可执行文件:C:\Python27\python.exe
Python版本:2.7.10
/ pre>

如何在所有模板中传递此表单?

解决方案

p>我不认为你理解上下文处理器的概念。这些是简单的功能,将东西添加到上下文,这是由django管理并发送到每个模板的字典。



上下文处理器允许您将自己的键和值添加到此字典,然后可以在所有模板中使用。



在上下文处理器中,您只需使用自定义键返回一个字典/值对 - 没有更多的东西,例如:

  from someapp.forms import ContactForm 

def ctx_contact_form (请求):
return {'contact-form':ContactForm()}

在所有模板中,您都可以使用 {{contact-form}} 。请记住,您仍然必须在模板中编写周围的HTML:

 < form method =postaction ={%url'your-view'%}> 
{{contact-form}}
< input type =submit>
< / form>

如果你想要甚至为你写的那部分,所以你必须在你的模板进入 {{contact-form}} ,那么你需要写一个自定义模板标签,其中涉及到几个步骤。



首先,创建一个简单的模板,让说 _formtag.html ,并包含上面的HTML:

  ; form method =postaction ={%url'your-view'%}> 
{{contact-form}}
< input type =submit>
< / form>

接下来,创建一个名为 templatetags 的目录您的应用程序内部创建一个名为 __ init __。py 的空文件,然后创建另一个文件,调用它 app_form_tags.py ;在这个 app_form_tags.py 文件中,添加以下代码:

  from django import template 
from yourapp.forms import ContactForm

register = template.Library()

@ register.inclusion_tag('_ formtag.html')
def contact_form():
return {'contact-form':ContactForm()}

最后,无论你想要显示哪个表单(在模板中),只需添加

  {%load app_form_tags%} 

在文件的顶部,然后 {%contact_form%} 你想要的表单。



你的其余代码 - 也就是处理表单,必须在视图中写入正常。


I have this code in context_processors.py

class ContactFormView(FormView):
    form_class = ContactForm
    template_name = "blog/contact.html"
    success_url = "/contact/"
    def form_valid(self,form):
       contact_name = form.cleaned_data.get('contact_name')
       contact_email = form.cleaned_data.get('contact_email')
       form_content  = form.cleaned_data.get('content','')
       try:
          send_mail(contact_name,form_content,contact_email,[settings.EMAIL_HOST_USER], fail_silently=False)
       except BadHeaderError:
          return HttpResponse('Invalid Header Found')
       return super(ContactFormView,self).form_valid(form)

I want to include this in all the views by using context processors.I am getting this error:

TypeError at /
__init__() takes exactly 1 argument (2 given)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.8.7
Exception Type: TypeError
Exception Value:    
__init__() takes exactly 1 argument (2 given)
Exception Location: C:\Python27\lib\site-packages\django-1.8.7-py2.7.egg\django\template\context.py in bind_template, line 241
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.10

How to pass this form in all the template?

解决方案

I don't think you understand the concept of context processors. These are simple functions add things to the context which is a dictionary that is managed by django and sent to each and every template.

Context processors allow you to add your own keys and values to this dictionary, which are then available across all your templates.

In a context processor you just return a dictionary with your custom key/value pair - nothing more, for example:

from someapp.forms import ContactForm

def ctx_contact_form(request):
    return {'contact-form': ContactForm()}

Now, in all templates you'll have {{ contact-form }} available. Keep in mind you'll still have to write the surrounding HTML in your templates:

<form method="post" action="{% url 'your-view' %}">
   {{ contact-form }}
   <input type="submit">
</form>

If you want even that part to be written for you, so all you have to do in your templates is enter {{ contact-form }}, then you need to write a custom template tag, which involves a few steps.

First, create a simple template, lets say _formtag.html, and include the HTML from above:

<form method="post" action="{% url 'your-view' %}">
       {{ contact-form }}
       <input type="submit">
</form>

Next, create a directory called templatetags in your application, inside it, create an empty file called __init__.py, then create another file call it app_form_tags.py; in this app_form_tags.py file, add the following code:

from django import template
from yourapp.forms import ContactForm

register = template.Library()

@register.inclusion_tag('_formtag.html')
def contact_form():
    return {'contact-form': ContactForm()}

Finally, wherever you want to show that form (in the templates), just add

{% load app_form_tags %}

at the top of the file, and then {% contact_form %} where you want the form to go.

The rest of your code - that is, to process the form, will have to be written in the views as normal.

这篇关于如何在django中的context_processors中包含基于类的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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