Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:41:48+00:00 2026-05-14T20:41:48+00:00

In order to avoid spamming, I would like to add a waiting time to

  • 0

In order to avoid spamming, I would like to add a waiting time to re-submit a form (i.e. the user should wait a few seconds to submit the form, except the first time that this form is submitted).

To do that, I added a timestamp to my form (and a security_hash field containing the timestamp plus the settings.SECRET_KEY which ensures that the timestamp is not fiddled with). This look like:

class MyForm(forms.Form):
    timestamp     = forms.IntegerField(widget=forms.HiddenInput)
    security_hash = forms.CharField(min_length=40, max_length=40, widget=forms.HiddenInput)
    # + some other fields..

    # + methods to build the hash and to clean the timestamp...
    # (it is based on django.contrib.comments.forms.CommentSecurityForm)

    def clean_timestamp(self):
        """Make sure the delay is over (5 seconds)."""
        ts = self.cleaned_data["timestamp"]
        if not time.time() - ts > 5:
            raise forms.ValidationError("Timestamp check failed")
        return ts

    # etc...

This works fine. However there is still an issue: the timestamp is checked the first time the form is submitted by the user, and I need to avoid this.

Any idea to fix it ?

Thank you ! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:41:48+00:00Added an answer on May 14, 2026 at 8:41 pm

    One way to do this is to set an initial value to time, let’s say 0, and update it to the current timestamp once the form validates, and only check timestamp when it’s not 0:

    class MyForm(forms.Form):
        timestamp     = forms.IntegerField(widget=forms.HiddenInput, initial=0)
        #look at the initial = 0
    
        security_hash = forms.CharField(min_length=40, max_length=40, widget=forms.HiddenInput)
    
    
        def clean_timestamp(self):
            """Make sure the delay is over (5 seconds)."""
            ts = self.cleaned_data["timestamp"]
            if timestamp != 0 and not time.time() - ts > 5:
                raise forms.ValidationError("Timestamp check failed")
            return ts
    
        def clean(self):
            cleaned_data = self.cleaned_data
            if len(self._errors) == 0: #it validates
                cleaned_data["timestamp"] = time.time()
            return cleaned_data
    

    Another possible solution is to use sessions. It’s safer but not bulletproof. With the previous apporach the user can send the same post data several times, and the form will validate several times (cause he’s sending the same timestamp). With sessions you need the user to have cookies enabled, but they won’t be able to send post data that validates quicker than 5 seconds.

    This way once a correct form submission happens you can save the time in a user’s session key and check that before revalidating your form. This is easily done in a view. If you want to do it at the form logic level, you need to create your own clean method in the form that takes the request (so you can use the session). Be careful, the user can clean his cookies and post as a “new” user.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to avoid content duplication, I would like to avoid the pages of
What are the best practices/idioms should someone follow in order to avoid deadlocks?
In order to handle concurrency issue, is locking-- any form of locking, whether it's
In order to avoid freezing of GUI, I wanted to run method connecting to
In order to avoid a huge hassle with ugly UI glitches from the popover's
I have added the following option in order to avoid having smaller multiple binary
DNS servers have to be fast in order to avoid latency. What algorithms do
The django books gives the local trick in order to avoid to type a
I have created several WCF services. In order to avoid adding service references in
In order to fully use LinqToSql in an ASP.net 3.5 application, it is necessary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.