SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Web Design Addict
    Join Date
    Mar 2004
    Posts
    2,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing widths of Django Admin forms

    Hi,

    Sorry if this is the wrong place to post this as I don't see a Django specific area.

    I'm new to Python and Django. Spent a few weeks getting to know Python; went through "Python in a Day" and about half of "Python the Hard Way" before I dove into Django. I'm understanding a lot of things, but still plenty that is over my head.

    Currently I've got a Blog app set up and my models.py completed. Problem is, the form fields are all about 200px wide. I'd like to customize the fields with custom widths and cols/rows for textareas.

    Here's my current admin.py file:

    Code Python:
    from django.contrib import admin
    from blog.models import Post
    from django import forms
     
    class PostAdminForm(forms.ModelForm):
    	title = forms.CharField(widget=forms.TextInput(attrs={'size':100}))
     
    class PostAdmin(admin.ModelAdmin):
    	list_display = ('title', 'status', 'pub_date', 'image', 'summary')
    	list_filter = ['pub_date', 'status']
    	search_fields = ['title', 'summary', 'body']
    	date_hierarchy = 'pub_date'
     
    admin.site.register(Post, PostAdmin)

    You can see where I've tried to set a custom size for the title field. I get no errors or anything with this code yet the title field doesn't change in size. What am I missing?

    I also found this bit of code which was from the same thread as I found the above. The poster indicated that it was "better" in some way but I can't seem to make it work either:

    Code Python:
    def get_form(self, request, obj=None): 
    	form = super(EventAdmin,self).get_form(request, obj=None) 
    	name = forms.CharField(widget=forms.TextInput(attrs={'size': 
    	100, 'max_length':200}),required=True,) 
    	form.base_fields['name']=name 
    	return form
    Last edited by Mittineague; Sep 16, 2013 at 21:54. Reason: reformatting bbcode tags
    Deron Sizemore
    ----------
    My Sites: LogoGala | Golf Ledger (coming soon)
    Twitter: Deron Sizemore

  2. #2
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Does the rendered HTML show actual size attributes showing up?

    Secondly, I believe that even with inline size attributes, CSS can override those by setting inputs to particular widths. So this might be a non-issue with the size but it would be good to know about the max-lengths for example, since form validators sometimes make use of them.

    We use WTForms at work and I'm always wasting time trying to figure out how to add attributes and edge cases I need to them. Generated forms are my enemy. But often I find how a lot of things are defaulted by looking at the main forms classes, so you'd want to look at where Django imports "forms" and see what's defined in for example the widgets.

  3. #3
    Web Design Addict
    Join Date
    Mar 2004
    Posts
    2,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    Does the rendered HTML show actual size attributes showing up?
    Good question. Not sure. Never thought to view source and check that.

    Quote Originally Posted by Stomme poes View Post
    Secondly, I believe that even with inline size attributes, CSS can override those by setting inputs to particular widths. So this might be a non-issue with the size but it would be good to know about the max-lengths for example, since form validators sometimes make use of them.
    Yes, that's what I found out browsing today trying to again to fix it. That CSS will override any size attribute. Seems like creating my own custom "static" CSS file and including that file into the admin.html template is the way to go for how to size these fields. I'm not exactly sure how to do that, but I'm going to give it a shot this evening.

    Quote Originally Posted by Stomme poes View Post
    But often I find how a lot of things are defaulted by looking at the main forms classes, so you'd want to look at where Django imports "forms" and see what's defined in for example the widgets.
    Can you tell me where to find the main forms classes? Sorry, I'm pretty new to this and I'm not sure what you're referring to.

    Thanks!
    Deron Sizemore
    ----------
    My Sites: LogoGala | Golf Ledger (coming soon)
    Twitter: Deron Sizemore

  4. #4
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    I don't know django but I'm referring to this
    from django import forms

    forms could either be a class or a module (.py file).

    However someone in the house has the 2 scoops of Django book so I may run through it quickly to see if they mention forms, and if so, where they tend to sit.

  5. #5
    Web Design Addict
    Join Date
    Mar 2004
    Posts
    2,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    I don't know django but I'm referring to this
    from django import forms

    forms could either be a class or a module (.py file).

    However someone in the house has the 2 scoops of Django book so I may run through it quickly to see if they mention forms, and if so, where they tend to sit.
    Thanks. I've actually got that book somewhere I think. I forgot I had it. I'll check it also.
    Deron Sizemore
    ----------
    My Sites: LogoGala | Golf Ledger (coming soon)
    Twitter: Deron Sizemore


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •