渲染时捕获AttributeError:'WSGIRequest'对象没有属性'get'

时间:2011-07-28 00:43:51

标签: django forms

关于表格的更多信息。我终于得到了我的表单来验证,发布和重定向到它需要的页面。现在我的问题是,当我返回页面时,我收到此错误:

渲染时捕获AttributeError:'WSGIRequest'对象没有属性'get'

似乎恢复它工作的唯一方法是删除forms.py替换之前无效的。添加什么工作,我可以让它工作ONCE。什么可以导致这个问题的想法?

形式:

class LeadSubmissionForm(forms.ModelForm):
    """
    A simple default form for messaging.
    """
    class Meta:
         model = Lead
        fields = ( 
            'parent_or_student', 'attending_school', 'how_much', 'year_of_study', 'zip_code', 'email_address', 'graduate_year', 'graduate_month', 'email_loan_results'
        )

视图:

@render_to("lender/main_views/home.html")
def home(request):
    if request.method == 'POST':
        form = LeadSubmissionForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("search_results"))
    else:
        form = LeadSubmissionForm(request)

    testimonials = Testimonial.objects.filter(published=True)[:3]
    return {'lead_submission_form':form,
        'testimonials': testimonials,}

模型:

class Lead(TitleAndSlugModel):
    """
    A lead submitted through the site (i.e. someone that has at-least submitted the search form
    """

    PARENT_OR_STUDENT = get_namedtuple_choices('PARENT_OR_STUDENT', (
        (0, 'PARENT', 'Parent'),
        (1, 'STUDENT', 'Student'),
    ))
    YEARS_OF_STUDY = get_namedtuple_choices('YEARS_OF_STUDY', (
        (0, 'ONE', '1'),
        (1, 'TWO', '2'),
        (2, 'THREE', '3'),
        (3, 'FOUR', '4'),
    ))

    parent_or_student = models.PositiveIntegerField(choices=PARENT_OR_STUDENT.get_choices(), default=0)
    attending_school = models.ForeignKey(School)
    how_much = models.DecimalField(max_digits=10, decimal_places=2)
    year_of_study = models.PositiveIntegerField(choices=YEARS_OF_STUDY.get_choices(), default=0)
    zip_code = models.CharField(max_length=8)
    email_address = models.EmailField(max_length=255)
    graduate_year = models.IntegerField()
    graduate_month = models.IntegerField()
    email_loan_results = models.BooleanField(default=False)

    def __unicode__(self):
        return "%s - %s" % (self.email_address, self.attending_school)

再一次任何帮助都是很有帮助的。谢谢!!

1 个答案:

答案 0 :(得分:20)

当request.method =='GET'时实例化LeadSubmissionForm时,您不需要传递请求。

要保存几行代码,您还可以执行以下操作:

@render_to("lender/main_views/home.html")
def home(request):
    form = LeadSubmissionForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("search_results"))
    testimonials = Testimonial.objects.filter(published=True)[:3]
    return {'lead_submission_form':form, 'testimonials': testimonials}

希望能帮到你。

相关问题