I am an absolute novice to django-cms. I have gone through the tutorial and configured it exactly the same as mentioned in the documentation. Now, I have to build an application which uses a form to upload products.
我是django-cms的绝对新手。我已经浏览了本教程,并将其配置为与文档中提到的完全相同。现在,我必须构建一个应用程序,它使用一个表单来上传产品。
I dont have a clue as to how to move ahead with it. I want to start it with simple forms as if now, say, a username and password textbox kind of. How can I use django forms in the django-cms page? I have snippet plugin enabled in it too. I need some guidance for this.
我不知道如何推进它。我想从简单的表单开始,比如用户名和密码文本框。如何在django-cms页面中使用django表单?我也有代码片段插件。我需要一些指导。
Any suggestions plsss. thanks
plsss任何建议。谢谢
6 个解决方案
#1
5
I've recently ran across django-form-designer
我最近遇到了django窗体设计器
It's quite nice and lets you design forms in a wysiwyg manner. Works for stand alone django applications and has an additional plugin for Django-CMS. One note: you have to follow the standalone instructions in addition to the Django-CMS instructions for installation.
这是相当不错的,让您设计的表单以一种所见即所得的方式。适用于单独的django应用程序,还有一个用于django - cms的插件。注意:除了安装Django-CMS指令之外,您还必须遵循独立指令。
From the github description:
github的描述:
Key features:
主要特点:
- Design contact forms, search forms etc from the Django admin, without writing any code
- 从Django管理员设计联系人表单、搜索表单等,不编写任何代码
- Form data can be logged and CSV-exported, sent via e-mail, or forwarded to any web address
- 可以记录表单数据并导出csv、通过电子邮件发送或转发到任何web地址
- Integration with Django CMS: Add forms to any page
- 与Django CMS集成:向任何页面添加表单
- Use drag & drop to change the position of your form fields
- 使用拖放更改表单字段的位置
- Fully collapsible admin interface for better overview over your form
- 完全可折叠的管理界面,以更好地概述您的表单
- Implements many form fields included with Django (TextField, EmailField, DateField etc)
- 实现Django包含的许多表单字段(TextField、EmailField、DateField等)
- Validation rules as supplied by Django are fully configurable (maximum length, regular expression etc)
- Django提供的验证规则是完全可配置的(最大长度、正则表达式等)
- Customizable messages and labels
- 可定制的消息和标签
- Supports POST and GET forms
- 支持POST和GET表单
#2
15
Actually the solution proposed by bennylope is not the preferred way to do it, since using request.POST in a plugin can have very bad side effects (eg: what if the same plugin is twice on a page? Or what if there's multiple plugins waiting for POST data on the same page? Those plugins would confuse each other when there's a POST to that page).
实际上,bennylope提出的解决方案并不是首选的方法,因为使用request。在插件中发布会有非常糟糕的副作用(例如:如果同一个插件在一个页面上出现两次会怎么样?)或者,如果有多个插件等待在同一页面上发布数据怎么办?这些插件在页面上发布时会相互混淆)。
So the preferred way is:
所以首选的方法是:
- Make a CMSPlugin as described by bennylope to render the form.
- 按照bennylope的描述制作一个CMSPlugin来呈现表单。
- Make the form post to a statically hooked view (or a apphooked view), if the POST is successful, redirect to the page again if you want.
- 将表单post设置为静态挂接视图(或apphotourview),如果该post成功,如果需要,可以重定向到页面。
#3
6
Assuming your product application works as expected without Django CMS, what you'd next want to do is create your own plugin to show the form. The plugin would render your form, which you've already defined in your own application using a plugin template that you've created.
假设您的产品应用程序在没有Django CMS的情况下正常工作,那么您接下来要做的就是创建自己的插件来显示表单。该插件将呈现您的表单,您已经在自己的应用程序中使用创建的插件模板定义了表单。
This plugin for a contact form allows the contact form to be inserted into the page template anywhere a placeholder will allow it.
此联系人表单插件允许在占位符允许的任何位置将联系人表单插入页面模板。
class ContactPlugin(CMSPluginBase):
"""Enables latest event to be rendered in CMS"""
model = CMSPlugin
name = "Form: Contact"
render_template = "contact_form/contact_plugin.html"
def render(self, context, instance, placeholder):
request = context['request']
context.update({
'instance': instance,
'placeholder': placeholder,
'form': ContactForm(request=request),
})
return context
The template would include all the HTML and Django template language necessary to render the form.
模板将包含呈现表单所需的所有HTML和Django模板语言。
This other contact form plugin shows another example of how to do it. Instead of rendering the form it just updates the context. The upside is that you don't have to create a separate template, but the downside is that you lose some of the modularity of having a plugin. This depends on the page template rendering the form.
另一个联系人表单插件显示了另一个示例。它不是呈现表单,而是更新上下文。优点是您不必创建单独的模板,但是缺点是您失去了一些插件的模块化。这取决于呈现表单的页面模板。
class ContactPlugin(CMSPluginBase):
model = Contact
name = _("Contact Form")
render_template = "contact.html"
def render(self, context, instance, placeholder):
request = context['request']
if request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
form.send(instance.site_email)
context.update( {
'contact': instance,
})
return context
else:
form = ContactForm()
context.update({
'contact': instance,
'form': form,
})
return context
In either case you still have to define the view to accept the form submission, which means you'll need to create a view outside of the CMS acknowledging receipt of the form with its own template, redirecting the user back to the referring page, and/or accepting an AJAX request.
无论哪种情况,您仍然需要定义视图来接受表单提交,这意味着您需要在CMS之外创建一个视图,以确认表单的接收和它自己的模板,将用户重定向回引用页面,以及/或接受AJAX请求。
#4
5
I'm new to django-cms too, but the way ojii described seems like the most reasonable:
我也是django-cms的新手,但是ojii描述的方式似乎是最合理的:
- Make a CMSPlugin as described above to render the form.
- 制作上面描述的CMSPlugin以呈现表单。
- Make the form post to a statically hooked view (or a apphooked view), if the POST is successful, redirect to the page again if you want.
- 将表单post设置为静态挂接视图(或apphotourview),如果该post成功,如果需要,可以重定向到页面。
However for me this only a partial solution because I don't only want to redirect back to the cms page containing the plugin in the case of success but also in the case of form errors which need to be displayed. The redirecting part is not the problem (I just redirect back to request.META["HTTP_REFERER"]), but the part of handing over the form (errors) to the cms page containing the plugin is tricky. The only answer I found for this: I use a session (Writing the form into the session in the static view, and then deleting it again in the plugin's render method). Not the nicest way, but it works. (There would be better ways, if the cms plugins configuration would support some way to redirect instead of rendering, but apparently there is no such option: see http://groups.google.com/group/django-cms/browse_thread/thread/79ab6080c80bbcb5 )
但是对我来说,这只是一个部分解决方案,因为我不仅想在成功的情况下重定向到包含插件的cms页面,而且还想在需要显示的表单错误中重定向。重定向部分不是问题所在(我只是重定向到request.META["HTTP_REFERER"]),但是将表单(错误)提交到包含插件的cms页面是很棘手的。我找到的唯一答案是:我使用一个会话(在静态视图中将表单写到会话中,然后在插件的呈现方法中再次删除它)。这不是最好的方法,但确实有效。(如果cms插件配置支持重定向而不是呈现,可能会有更好的方法,但显然没有这样的选项:参见http://groups.google.com/group/django- cms/browse_thread/79ab6080c80bcb5)
#5
2
This might be way too late but I had a similar problem so I created a more general plugin: https://github.com/metzlar/cms-form-plugin which happens to be very similar to bennylope's answer.
这可能太晚了,但是我有一个类似的问题,所以我创建了一个更通用的插件:https://github.com/metzlar/cms-form-plugin,这个插件与bennylope的答案非常相似。
#6
1
You can work around by throwing an exception and catching it in middleware: http://djangosnippets.org/snippets/2541/
您可以抛出一个异常并在中间件中捕获它:http://djangosnippets.org/snippets/2541/
#1
5
I've recently ran across django-form-designer
我最近遇到了django窗体设计器
It's quite nice and lets you design forms in a wysiwyg manner. Works for stand alone django applications and has an additional plugin for Django-CMS. One note: you have to follow the standalone instructions in addition to the Django-CMS instructions for installation.
这是相当不错的,让您设计的表单以一种所见即所得的方式。适用于单独的django应用程序,还有一个用于django - cms的插件。注意:除了安装Django-CMS指令之外,您还必须遵循独立指令。
From the github description:
github的描述:
Key features:
主要特点:
- Design contact forms, search forms etc from the Django admin, without writing any code
- 从Django管理员设计联系人表单、搜索表单等,不编写任何代码
- Form data can be logged and CSV-exported, sent via e-mail, or forwarded to any web address
- 可以记录表单数据并导出csv、通过电子邮件发送或转发到任何web地址
- Integration with Django CMS: Add forms to any page
- 与Django CMS集成:向任何页面添加表单
- Use drag & drop to change the position of your form fields
- 使用拖放更改表单字段的位置
- Fully collapsible admin interface for better overview over your form
- 完全可折叠的管理界面,以更好地概述您的表单
- Implements many form fields included with Django (TextField, EmailField, DateField etc)
- 实现Django包含的许多表单字段(TextField、EmailField、DateField等)
- Validation rules as supplied by Django are fully configurable (maximum length, regular expression etc)
- Django提供的验证规则是完全可配置的(最大长度、正则表达式等)
- Customizable messages and labels
- 可定制的消息和标签
- Supports POST and GET forms
- 支持POST和GET表单
#2
15
Actually the solution proposed by bennylope is not the preferred way to do it, since using request.POST in a plugin can have very bad side effects (eg: what if the same plugin is twice on a page? Or what if there's multiple plugins waiting for POST data on the same page? Those plugins would confuse each other when there's a POST to that page).
实际上,bennylope提出的解决方案并不是首选的方法,因为使用request。在插件中发布会有非常糟糕的副作用(例如:如果同一个插件在一个页面上出现两次会怎么样?)或者,如果有多个插件等待在同一页面上发布数据怎么办?这些插件在页面上发布时会相互混淆)。
So the preferred way is:
所以首选的方法是:
- Make a CMSPlugin as described by bennylope to render the form.
- 按照bennylope的描述制作一个CMSPlugin来呈现表单。
- Make the form post to a statically hooked view (or a apphooked view), if the POST is successful, redirect to the page again if you want.
- 将表单post设置为静态挂接视图(或apphotourview),如果该post成功,如果需要,可以重定向到页面。
#3
6
Assuming your product application works as expected without Django CMS, what you'd next want to do is create your own plugin to show the form. The plugin would render your form, which you've already defined in your own application using a plugin template that you've created.
假设您的产品应用程序在没有Django CMS的情况下正常工作,那么您接下来要做的就是创建自己的插件来显示表单。该插件将呈现您的表单,您已经在自己的应用程序中使用创建的插件模板定义了表单。
This plugin for a contact form allows the contact form to be inserted into the page template anywhere a placeholder will allow it.
此联系人表单插件允许在占位符允许的任何位置将联系人表单插入页面模板。
class ContactPlugin(CMSPluginBase):
"""Enables latest event to be rendered in CMS"""
model = CMSPlugin
name = "Form: Contact"
render_template = "contact_form/contact_plugin.html"
def render(self, context, instance, placeholder):
request = context['request']
context.update({
'instance': instance,
'placeholder': placeholder,
'form': ContactForm(request=request),
})
return context
The template would include all the HTML and Django template language necessary to render the form.
模板将包含呈现表单所需的所有HTML和Django模板语言。
This other contact form plugin shows another example of how to do it. Instead of rendering the form it just updates the context. The upside is that you don't have to create a separate template, but the downside is that you lose some of the modularity of having a plugin. This depends on the page template rendering the form.
另一个联系人表单插件显示了另一个示例。它不是呈现表单,而是更新上下文。优点是您不必创建单独的模板,但是缺点是您失去了一些插件的模块化。这取决于呈现表单的页面模板。
class ContactPlugin(CMSPluginBase):
model = Contact
name = _("Contact Form")
render_template = "contact.html"
def render(self, context, instance, placeholder):
request = context['request']
if request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
form.send(instance.site_email)
context.update( {
'contact': instance,
})
return context
else:
form = ContactForm()
context.update({
'contact': instance,
'form': form,
})
return context
In either case you still have to define the view to accept the form submission, which means you'll need to create a view outside of the CMS acknowledging receipt of the form with its own template, redirecting the user back to the referring page, and/or accepting an AJAX request.
无论哪种情况,您仍然需要定义视图来接受表单提交,这意味着您需要在CMS之外创建一个视图,以确认表单的接收和它自己的模板,将用户重定向回引用页面,以及/或接受AJAX请求。
#4
5
I'm new to django-cms too, but the way ojii described seems like the most reasonable:
我也是django-cms的新手,但是ojii描述的方式似乎是最合理的:
- Make a CMSPlugin as described above to render the form.
- 制作上面描述的CMSPlugin以呈现表单。
- Make the form post to a statically hooked view (or a apphooked view), if the POST is successful, redirect to the page again if you want.
- 将表单post设置为静态挂接视图(或apphotourview),如果该post成功,如果需要,可以重定向到页面。
However for me this only a partial solution because I don't only want to redirect back to the cms page containing the plugin in the case of success but also in the case of form errors which need to be displayed. The redirecting part is not the problem (I just redirect back to request.META["HTTP_REFERER"]), but the part of handing over the form (errors) to the cms page containing the plugin is tricky. The only answer I found for this: I use a session (Writing the form into the session in the static view, and then deleting it again in the plugin's render method). Not the nicest way, but it works. (There would be better ways, if the cms plugins configuration would support some way to redirect instead of rendering, but apparently there is no such option: see http://groups.google.com/group/django-cms/browse_thread/thread/79ab6080c80bbcb5 )
但是对我来说,这只是一个部分解决方案,因为我不仅想在成功的情况下重定向到包含插件的cms页面,而且还想在需要显示的表单错误中重定向。重定向部分不是问题所在(我只是重定向到request.META["HTTP_REFERER"]),但是将表单(错误)提交到包含插件的cms页面是很棘手的。我找到的唯一答案是:我使用一个会话(在静态视图中将表单写到会话中,然后在插件的呈现方法中再次删除它)。这不是最好的方法,但确实有效。(如果cms插件配置支持重定向而不是呈现,可能会有更好的方法,但显然没有这样的选项:参见http://groups.google.com/group/django- cms/browse_thread/79ab6080c80bcb5)
#5
2
This might be way too late but I had a similar problem so I created a more general plugin: https://github.com/metzlar/cms-form-plugin which happens to be very similar to bennylope's answer.
这可能太晚了,但是我有一个类似的问题,所以我创建了一个更通用的插件:https://github.com/metzlar/cms-form-plugin,这个插件与bennylope的答案非常相似。
#6
1
You can work around by throwing an exception and catching it in middleware: http://djangosnippets.org/snippets/2541/
您可以抛出一个异常并在中间件中捕获它:http://djangosnippets.org/snippets/2541/