2012-05-07 59 views
-3

我有一个关于request.Get在Django的基本问题。我已经在Django request.Get传递变量到下一页

以下给出了我的表单声明现在,我需要将v.position传递到下一页。如何验证request.get如果我的v.position已填充

例如: - 下面的代码是否正确?

volpostion = request.GET中[ '志愿者位置']。条()

如果volposition 执行所需的功能。

我读了那个request.Get获取一个表单字段的字典..表单字段在这里表示标签名?

Forms.py

<tr> 
     <th colspan=4 align="left"><label for="id_Volposition">Volunteer Position:</label></th> 
     <th colspan=.5 align="left"><a href="/signups/new/{{ v.position }}" class="username" <u>{{ v.volposition }}</u></a></th> 
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr> 
</tr> 

<tr> 
     <th colspan=4 align="left"><label for="id_roledesc">Role Description:</label></th> 
     <th colspan=.5 align="left">{{ v.roledesc }}</th> 
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr> 
</tr> 

的更多信息:

urls.py

urlpatterns = patterns('', 
    (r'^new/$',       sfp.view), 
    (r'^volunteer/$',  volunteer_page), 
    (r'^vollist/$', volunteer_list), 
    (r'^volcont/$', volunteer_contact) 

views.py

sfp = SimpleFormProcessing(
    form_class=VolunteerSignupForm, 
    form_2_model=volunteersignupform_2_model, 
    form2_class=VolunteerForm, 
    form_template='signups/create_contact_form.dmpl', 
    email_template='signups/response_email.dmpl', 
    email_html_template='signups/response_email_html.dmpl', 
    email_subject='Vibha Volunteer Signup', 
    email_sender='[email protected]', 
    redirect_url='/signups/thanks/', 
    do_captcha=True) 

SimpleFormProcessing.py

from django.shortcuts import render_to_response 
from django.core.mail import EmailMultiAlternatives 
from django.template.loader import render_to_string 
from django.http import HttpResponseRedirect 
from formwrappers import form_with_captcha, form_with_ipaddress 
import pdb 


class SimpleFormProcessing: 

    def __init__(self, form_class, form_2_model, form2_class, form_template, 
      email_template, email_subject, email_sender, redirect_url, 
      do_captcha=False, record_ip_addr=False, email_html_template=None): 
     self.form_class = form_class 
     self.form_2_model = form_2_model 
     self.form_template = form_template 
     self.email_template = email_template 
     self.email_html_template = email_html_template 
     self.email_subject = email_subject 
     self.email_sender = email_sender 
     self.redirect_url = redirect_url 
     self.do_captcha = do_captcha 
     self.record_ip_addr = record_ip_addr 

    def view(self, request, initial={}): 
     pdb.set_trace() 
     Form = self.form_class 
     if self.do_captcha: 
      Form = form_with_captcha(Form, request) 
     if self.record_ip_addr: 
      Form = form_with_ipaddress(Form, request) 
     if request.method == 'POST': 
      # Try processing the form 
      if self.do_captcha and not accepts_cookies(request): 
       return our_flatpage('Please enable cookies and try again.') 
      else: 
       form = Form(request.POST) 
       if form.is_valid(): 
        # The form is correct, process it 
        model = self.form_2_model(form) 
        if self.email_template: 
         text_content = render_to_string(self.email_template, {'model': model}) 
         recipients = model.emailRecipients() 
         try: 
          bcc_recipients = model.emailBCCRecipients() 
         except: 
          bcc_recipients = None 
         msg = EmailMultiAlternatives(self.email_subject, text_content, self.email_sender, 
           recipients, bcc_recipients) 

         if self.email_html_template: 
          html_content = render_to_string(self.email_html_template, {'model': model}) 
          msg.attach_alternative(html_content, "text/html") 

         msg.send() 

        return HttpResponseRedirect(self.redirect_url) 
       else: 
        # Show the form with errors 
        return render_to_response(self.form_template, {'form': form}) 
     else: 
      # Show the empty form 

      form = Form(initial=initial) 
      if self.do_captcha: 
       accepts_cookies(request) 

volunteer_list.dmpl

{% extends "base.dmpl" %} 
{% block title %}User Registration{% endblock %} 
{% block head %}User Registration{% endblock %} 
{% block contentBox %} 
<h2>Volunteer Opportunities</h2> 
<p>Vibha is currently looking for volunteers interested in helping the organization in the following areas:</p> 
<div class="relation"> 
<ul> 
{% for k in teamrel %} 
     <li align="left"><a href="#{{ k }} "> <b> {{ k}} </b> </a> </li> 
{% endfor %} 
</ul> 
</div> 

<p>If you are interested in any of these opportunities please contact us by clicking on the position you are interested and filling out the form.</p> 
<p>&nbsp;</p> 

<table> 

{% for v in vollist %} 
{% ifchanged v.teamrelation %} 
<tr> 
<th colspan=7 align="left"><h3><a name="{{ v.teamrelation }}"> {{v.teamrelation}} </a> </h3></th> 
</tr> 
{% endifchanged %} 


<tr> 
     <th colspan=4 align="left"><label for="id_Volposition">Volunteer Position:</label></th> 
     <th colspan=.5 align="left"><a href="/signups/new/{{ v.position }}" class="username" <u>{{ v.volposition }}</u></a></th> 
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr> 
</tr> 

<tr> 
     <th colspan=4 align="left"><label for="id_roledesc">Role Description:</label></th> 
     <th colspan=.5 align="left">{{ v.roledesc }}</th> 
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr> 
</tr> 
<tr> 
     <th colspan=4 align="left"><label for="id_timereqt">Time Requirements:</label></th> 
     <th colspan=.5 align="left">{{ v.noofhours }} hours per month</th> 
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr> 
</tr> 
<tr> 
     <th colspan=4 align="left"><label for="id_qualreqt">Qualification and Requirements:</label></th> 
     <th colspan=.5 align="left">{{ v.Qualreqt }}</th> 
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr> 
</tr> 
<tr> 
     <th colspan=4 align="left"><label for="id_duration">Duration of the Role:</label></th> 
     <th colspan=20 align="left"> {{ v.Duration}} {{ v.Durationyrmon}}</th> 
<tr> <td colspan="10" height="20" style="display:none">&nbsp;</td> </tr> 
</tr> 
{% endfor %} 

</table> 

{% endblock %} 
+0

你的'forms.py'包含html?我对你的问题既没有形式也没有django的形式。什么是“v”。我们应该知道吗? – rantanplan

+0

使用request.session – mossplix

+0

对不起,我没有使用form ..它是一个html页面..志愿者的职位将通过。我需要知道如果访问志愿者的位置,如果它充满了意见。 – user1050619

回答

2

你的问题是相当困难的破译,但是从什么我可以做的是,这里的一些指针:

  1. 的按键既request.GETrequest.POST字典(取决于使用哪种方法)将是字段的HTML name属性,而不是它们的标签。你没有张贴任何代码,包括任何领域,但在一般情况下,如果你有这样的事情:

    <input type="hidden" name="volposition" value="0"> 
    

    然后,你会发现与价值:

    request.POST['volposition'] 
    
  2. 始终使用.get()方法在字典中,当你不确定该键是否在那里。如果密钥不存在,您的代码将引发IndexError异常。例如:

    volposition = request.GET.get('volposition') 
    
  3. 形式应该几乎总是可通过POST方法发送。 罕见的异常是像搜索表单这样的事情,其中​​提交的值作为查询字符串更有意义(通常通过GET方法发送)。经验法则是,如果您要求只读资源,请使用GET。例如,搜索谷歌,不会更改什么,你只是要求一个搜索结果列表,所以使用GET。但是,如果您正在进行任何类型的修改(创建,编辑甚至只是更新会话数据......任何实际上会影响某种更改)然后您使用POST。你也不应该混淆两者。例如,请勿将POST添加到包含查询字符串的网址。

就这么说。你应该可以用下面的覆盖你的基地:

{% if request.REQUEST.volposition %} 
<input type="hidden" name="volposition" value="{{ request.REQUEST.volposition }}"> 
{% endif %} 

request.REQUEST包含的URL传递的参数,无论是GETPOST。总是使用request.GETrequest.POST当你知道数据将通过什么方式发送,但在这种情况下,你只需要这个值,它可以来自任何一种方法,request.REQUEST是可以接受的。

如果密钥确实存在,则隐藏的输入将与其值一起呈现。无论数据如何发送,volposition现在都会被传送。

+0

多谢克里斯..这正是我正在寻找.. – user1050619

+0

作为一般的说明,它被认为是一种好的形式upvote /接受你的答案 - 这样的问题被认为是答案,用户得到的荣誉为他们的工作。 – jvc26