urls.py:
url( r'^Feedback/$','tool.vIEws.Feedback',name='Feedback'),url( r'^thanks/$',direct_to_template,{ 'template': 'tool_Feedback_thanks.HTML' },name='Feedback_thanks'),
forms.py:
class FeedbackForm(forms.Form): yes_no = forms.ChoiceFIEld( choices=YES_NO_CHOICE,initial=1,Widget=forms.RadioSelect(attrs={'class': 'can_reveal_input'}),label="Are you happy with Our service?" ) comments = forms.CharFIEld( Widget=forms.Textarea(attrs={ 'class': 'hIDden','placeholder': 'Leave us your comments...' }),required=False,label="" )
vIEws.py:
def Feedback(request,template_name='tool_Feedback.HTML'): Title = u'Leave us some Feedback' form = FeedbackForm(request.POST or None) if form.is_valID(): yes_no = form.cleaned_data['yes_no'] comments = form.cleaned_data['comments'] sender = "A Unirac website user" recipIEnts = ['person@example.com'] send_mail(yes_no,comments,sender,recipIEnts) return httpResponseRedirect( reverse('Feedback_thanks') ) return render_to_response(template_name,{ 'Title': Title,'form': form,},RequestContext(request))
这是一种享受,但现在客户要求每个页面都包含此表单.我想这个表单可以通过Js提交到相应的url,但是在每个页面上包含未绑定表单的最佳方法是什么?
任何帮助将非常感激.
解决方法 我将创建一个上下文处理器,以在每个视图中包含该表单.编辑:
要让用户访问他/她正在浏览的上一个网址,您只能使用网址.
# yourapp/context_processors.pydef Feedback_form_context_processor(request): return { 'Feedback_form': FeedbackForm(),'Feedback_form_url': reverse("Feed_app:form_process",args=(request.path)) }
这就是urls.py的样子:
urlpatterns = patterns('Feed_app.vIEws',url(r'^process-Feedback-form/(?P<next_url>\d+)','form_process',name='form_process'),)
以及表单的视图:
def form_process(request,next_url): # Process form,do your stuff here # if its valID redirect to the url return redirect(next_url)
您应该构建模板以获得正确的布局.例如,有一个基本模板:
# templates/base.HTML<HTML><body>..{% block maincontent %}{% endblock %}..{# The form!!! #}<form action='{{Feedback_form_url}}' method='POST'>@csrftoken{{ Feedback_form.as_p }}</form></body></HTML>
要创建简单视图,只需使用正确的模板即可.
# templates/just_a_random_vIEw.HTML{% extends base.HTML %}{% block maincontent %}<h1>Content!</h1>{% endblock %}
最后,将其包含在您的设置中:
# settings.pyTEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth",... "yourapp.context_processors.Feedback_form_context_processor")总结
以上是内存溢出为你收集整理的Django在每个页面上添加反馈表单全部内容,希望文章能够帮你解决Django在每个页面上添加反馈表单所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)