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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:03:30+00:00 2026-06-11T06:03:30+00:00

How do I correctly provide a custom widget with attrs? The call print attrs

  • 0

How do I correctly provide a custom widget with attrs? The call print attrs only outputs this:

attrs in LockFlagWidget are:
{'id': u'id_lockedFlag'}

whereas I would expect this:

attrs in LockFlagWidget are:
{'id': u'id_lockedFlag', 'size': u'80}

I wrote a custom widget like this:

class LockFlagWidget(forms.widgets.Widget):

    def render(self, name, value, attrs=None):
        if value is None:
                value = ''
        print "attrs in LockFlagWidget are:"
        print attrs
        staticURL = getattr(settings, 'STATIC_URL')

        if value != '':
            if value == True:
                return mark_safe(u"<img src=\"" + staticURL +
                                 "lock/img/lock_closed.ico\"/>"
                                 + "<span> - Is locked</span>")
            else:
                return mark_safe(u"<img src=\"" + staticURL +
                                 "lock/img/lock_open.ico\"/>"
                                 + "<span> - Is not locked</span>")

Now I m using the widget in a form like this:

class LockForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(LockForm, self).__init__(*args, **kwargs)
        self.fields['lockedFlag'].widget = LockFlagWidget(attrs={'size':'80'})

MyModel looks like this:

class MyModel(models.Model):
    lockedFlag = models.BooleanField(default=False)
  • 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-06-11T06:03:32+00:00Added an answer on June 11, 2026 at 6:03 am

    It looks to my like the attributes that are being passed into your render method are the attributes from the field not the widget, try to change your print statement to print self.attrs, that will pick up the attributes that you passed into the constructor of your widget. Hope that makes sense?

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

Sidebar

Related Questions

My company uses the MSBuild Community Tasks to provide custom build scripts to our
So far, my TwigBundle custom error404.html.twig page displays correctly on production mode when I
I've only been able to find a couple of people online mentioning this, and
I have a custom Qt widget which I used to display disassembly and I
This is similar to Dependency Injection with Custom Membership Provider , but the responses
I have a layer-hosting view set up like this in a custom NSView subclass:
I have written a Wordpress widget that displays a custom amount of posts from
It would be great if the Petrel Plugin Manager could display our custom bitmap
I recently encountered a problem with my Profile provider: it wouldn't retrieve profiles correctly
currently I used SetUnhandledExceptionFilter() to provide callback to get information when an unhandled exception

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.