每天只允许表单提交django

时间:2015-07-23 15:09:09

标签: python django forms

我想允许用户提交一次django表单,每天只提交一次。提交表单后,表单甚至不显示(服务器端检查,我不想使用JS或客户端的东西;很容易'篡改')

在Django中这样做的最佳方式是什么?

我尝试了什么?

我还没有尝试过,但是,我考虑过这个选项。

  • 触发一个布尔字段(用户提交的表单上的??或他/她的帐户?)更改为True,然后在午夜将该字段重置为False

通过这种方法,我想知道如何在午夜将字段重置为False。

我在Django Users上问了同样的问题,并提出了一些建议,所以我也在研究

2 个答案:

答案 0 :(得分:2)

已经有一段时间了,因为我问了这个问题,而且我想办法(不好的方式,也许)完成它。这是

的方式
#forms.py
class FormLastActiveForm(forms.ModelForm):
    class Meta:
        model = FormLastActive
        fields = '__all__'

# Create a model form for MyModel model below here

#models.py
class FormLastActive(models.Model):
    created_by = models.ForeignKey(User, blank=True, null=True)

class MyModel(models.Model):
    name = models.CharField(max_length=300)
    remark = models.CharField(max_length=300)

#views.py
schedule.every().day.at("00:00").do(reset_formlastactive) # will explain this in a bit too
def homepage(request):
    schedule.run_pending() # will explain this in a bit below
    if request.method == 'POST':
        form1 = MyModelForm(request.POST, prefix='form1', user=request.user)
        form2 = FormLastActiveForm(request.POST, prefix='form2')
        if form1.is_valid() and form2.is_valid():
            form1.instance.created_by = request.user
            form1.save()
            form2.instance.created_by = request.user
            form2.save()
            return HttpResponseRedirect('/thanks/')
    else:
        form1 = GhanaECGForm(prefix='form1')
        form2 = FormLastActiveForm(prefix='form2')

    active = None
    if request.user.is_authenticated():
        form_is_active = FormLastActive.objects.filter(created_by=request.user).exists()
        if is_active:
            active = True #if there exist a record for user in context
        else:
            active = False

    return render(request, 'index.html', {
            'first_form': form1,
            'second_form': form2,
            'no_form': active, # this triggers display of entire form or not
            }
        )


# index.html
<form action="." method="POST">
    {% csrf_token %} 
    {% form form=first_form %} {% endform %} # Using material package for form styling. Could use {{ first_form.as_p }} too for default styling.
    <!-- {% form form=second_form %} {% endform %} # This part is intentionally hidden to the user, and doesn't show up in the template -->
    <input type="submit" href="#" value="Submit" />
</form>

提交后,隐藏的表单和可见的表单都会被提交,并在上面的views.py中处理。

由于允许用户每天提交一个表单,这意味着需要有一种方法可以在午夜重置FormLastActive模型,从而为每个用户提供一个新的开始。只要第一个人在每天午夜12点之后提出请求,就会发生这种重置。

以下是这种方法的关系:

# the function for resetting FormLastActive Model every midnight
def reset_formlastactive():
    '''Resets the database responsible for
        allowing form submissions'''
    x = FormLastActive.objects.all()
    x.delete()

schedule.every().day.at("00:00").do(reset_formlastactive)
# if you noticed, this came just before the `homepage()` function begun in the views.py. This set the stage for resetting everyday, at 00:00 GMT.

要实际进行重置,我们需要调用上面的函数,我们这样做是通过:

schedule.run_pending()

homepage()

中的views.py函数正下方所示

使用了run_pending(),因此,即使重置时间已过,并且homepage()还没有在午夜12点之后运行,只要发生这种情况,{{1}将确保每个积压重置都已完成。

我目前在logecg.khophi.co上使用这种方法

我希望有更好,更清洁和可重复使用的方式,但同时,这有效!。

我希望我提出的这个问题毕竟不值得贬值。希望django有一个内置的功能,适用于所有类型的表单。

答案 1 :(得分:1)

您需要向User课程添加信息,以便其中包含上次提交表单的时间。

在呈现表单并接受帖子的函数中,您可以检查用户当天是否未提交表单。如果他这样做,则会为GET而不是真实表单呈现错误视图,并在POST请求时生成错误。

验证POST并完全接受后,您将更新User课程中的日期信息。