每天仅允许一次表单提交

每天仅允许一次表单提交

本文介绍了每天仅允许一次表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户一次提交django表单,并且每天只能提交一次.提交表单后,表单甚至不会显示(服务器端检查,我不想使用JS或客户端内容;很容易篡改")

I want to allow users to submit a django form once, and only once everyday. After submitting the form, the form wouldn't even show (server-side checkings, I don't want to use JS or client side thing; easily 'tamper-able')

在Django中这样做的最好方法是什么?

What is the best way to do so in Django?

我尝试了什么?

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

I have not tried any yet, however, I've considered this options.

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

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

With this approach, I wonder how I can reset the field to False at midnight too.

我在 Django用户上问了同样的问题,并且提出了一些建议,所以我也在调查中

I asked the same question on Django Users, and a couple of suggestions have come in, so I'm looking into as well

推荐答案

已经有一段时间了,因为我问了这个问题,所以我想出了一种方法(也许是坏方法)来完成它.这是

Its been a while, since I asked this question, and I figured a way (bad way, maybe) to get it done. Here's how

#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中进行处理.

Upon submitting, the hidden form and and the visible form all get submitted, and are processed in the views.py above.

由于允许用户每天提交一份表单,因此这意味着需要有一种方法在午夜重置FormLastActive模型,以便为每个用户提供全新的起点.每当第一个人在每天的午夜12点之后发出请求时,所有情况都会重置.

Since users are allowed to submit one form per day, then it means there need to be a means to reset the FormLastActive model at midnight, to give every user a fresh start. This reset for all happens whenever the first person makes a request after 12 midnight every day.

以下是该方法的联系方式:

Here's how that approach ties in:

# 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.

要真正进行重置,我们需要调用上述函数,并通过以下方式进行调用:

To actually do the resetting, we need to call the above function, and we do so via:

schedule.run_pending()

views.py

使用 run_pending(),这样即使经过了重置时间,并且 homepage()都没有在午夜12点之后运行发生这种情况时, run_pending()将确保完成每个待办事项重置.

The run_pending() is used, so that even if the time to reset is passed, and the homepage() hasn't been run after 12 midnight, whenever that happens, the run_pending() will ensure every backlog resetting is done.

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

I'm currently using this approach at logecg.khophi.co

我希望有一种更好,更清洁和可重用的方式来做到这一点,但与此同时,它能起作用!

I hope there's a better, cleaner and reusable way of doing this, but in the mean time, this works!.

我希望我提出的这个问题根本不值得接受.挥舞着的django对于所有类型的表单都有一个内置的功能.

I hope to this question I asked doesn't deserve the downvotes after all. Wished django had an inbuilt feature of this for all types of forms.

这篇关于每天仅允许一次表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 17:22