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

  • SEARCH
  • Home
  • 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 6016477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:59:17+00:00 2026-05-23T02:59:17+00:00

HI, I want to validate my urls whether they are post or get with

  • 0

HI,

I want to validate my urls whether they are post or get with caring the proper data.So i want to validate these urls before they call to respective views.So i am willing to write the some kind of middleware between view and urls so that i can keep safe the system.I am not aware how do i pass the data through middleware code to view.In middle ware i will write the unittest code.which will validate the urls if valid then will pass to the respective view other wise happy to say 404 .So can any buddy suggest me how do i handle the case.Or may be their is another alternative best way to do this validation.

Thanks to all.

  • 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-23T02:59:18+00:00Added an answer on May 23, 2026 at 2:59 am

    You should really be checking for request type in your views, and not in a middleware. As I mentioned in the comments above, you can’t tell whether a request is a POST message from the URL alone, let alone determine what POST data it carries.

    Checking the request type within a view is very straight-forward — simple check that request.method is equal to "GET" or "POST".

    If you’re doing this often, a short cut would be to create a decorator which does this check for you. For example, the following decorator checks that a GET request was used to call this view, or else return an HttpResponseBadRequest object (status code 400):

    # untested code, use with care
    def require_GET(view_func):
        def wrap(request, *args, **kwargs):
            if request.method != "GET":
                return HttpResponseBadRequest("Expecting GET request")
            return view_func(request, *args, **kwargs)
        wrap.__doc__ = view_func.__doc__
        wrap.__dict__ = view_func.__dict__
        wrap.__name__ = view_func.__name__
        return wrap
    

    You can then simply prepend your view function with @require_GET and the check will be done whever the view is called. E.g.

    @require_GET
    def your_view(request):
        # ...
    

    You can do the same for POST.

    Here’s an example decorator checking for POST request which takes an optional list of fields that must be provided with the POST request.

    # again, untested so use with care.
    def require_POST(view_func, required_fields=None):
        def wrap(request, *args, **kwargs):
            if request.method != "POST":
                return HttpResponseBadRequest("Expecting POST request")
            if required_fields:
                for f in required_fields:
                    if f not in request.POST:
                        return HttpResponseBadRequest("Expecting field %s" % f)
            return view_func(request, *args, **kwargs)
        wrap.__doc__ = view_func.__doc__
        wrap.__dict__ = view_func.__dict__
        wrap.__name__ = view_func.__name__
        return wrap
    

    Use like this:

    @require_POST
    def another_view(request):
        # ...
    

    or:

    @require_POST(required_fields=("username", "password"))
    def custom_login_view(request):
        # ...
    

    Update

    OK, my bad. I’ve just reinvented wheel.

    Django already provides the @require_GET and @require_POST decorators. See django.views.decorators.http.

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

Sidebar

Related Questions

I'm trying to validate URL. I want to make these URLs pass the validation:
I want to validate a condition before doing the next step, but only raise
I want to validate below data using regex and python. Below is the dump
I want to validate Image Resolution before upload an image in PHP through Javascript.
My project requires me to validate a large number of web URLs. These URLs
I want to validate my text are while entering data. I had written code
I want to validate a phone number input. The phone numbers should be mix
I want to validate mathematical expressions using regular expression. The mathematical expression can be
I want to validate a form fields (text) input. It should only be a
I want to validate my email address through jquery unobtrusive validation . Like to

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.