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:

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:

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 

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.

Good question. Not sure. Never thought to view source and check that.

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.

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!

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.