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 6600789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:40:12+00:00 2026-05-25T18:40:12+00:00

In Django, is it possible to set the default value of a form field

  • 0

In Django, is it possible to set the default value of a form field to be tied to a request variable. e.g. request.META['REMOTE_ADDR'].

This is just an example, it’d like to be use any of the headers available in the request variable without passing it explicitly to the form.

Thanks.

  • 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-25T18:40:12+00:00Added an answer on May 25, 2026 at 6:40 pm

    No, it is not possible, since the request object is not globally available, and thus it is not available in the form unless explicitly passed in. The initial argument exists for exactly the problem you are trying to solve. You should probably use

    form = YourForm(..., initial={'your_field': request.META['REMOTE_ADDR'])
    

    in your view.

    Edit:

    If you like to pass in the request explicitly, you can use something like this, and then just pass request=request when you instantiates the form:

    class YourForm(forms.Form):
        def __init__(self, *args, **kwargs):
            self.request = kwargs.pop('request')
            super(YourForm, self).__init__(*args, **kwargs)
    
        # ... use self.request in clean etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to read a Django model's fields' options? For example, with the
Is it possible to have a variable number of fields using django forms? The
Is it possible to access a JNDI value from Tomcat's config from a Django
How (if it is possible) can set up a route in Django that would
I have a couple of Django models set up like this: class Group(models.model): name
Is it possible to set DEBUG=False for only a specific app in Django? Celery
Is it possible to dynamically set the 'extra' option in the Django Admin Inline?
Is there a Django ORM best practice for this SQL: REPLACE app_model SET field_1
Let's say I have an string variable called *magic_string* which value is set to
I am trying to get the best possible set up for developing my django

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.