Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to edit the HTML. There is no need for overriding fieldset.html or other admin templates.</p> <p>What you want to do is:</p> <ul> <li>Make sure you can select your label with a css selector;</li> <li>Add a CSS stylesheet.</li> </ul> <p>You can select your label by using the following css:</p> <pre><code>/* attribute selector selecting only the label with for="id_numInvestments" */ label[for="id_numInvestments"] { width: 300px; } </code></pre> <p>or </p> <pre><code>/* Applies to all labels in fieldset with class='field-numInvestments' */ .field-numInvestments label { width: 300px; } </code></pre> <p>Make a new css file somewhere in your static directory (/path/to/my.css). The contents of this style sheet should be one of the css selectors above.</p> <p>To include this stylesheet to the headers of the admin 'add' and 'change' pages add a 'class Media' to your ModelAdmin:</p> <p>admin.py</p> <pre><code>class MyModelAdmin(admin.ModelAdmin): ... class Media: css = { 'all': ('/path/to/my.css',) } admin.site.register(MyModel, MyModelAdmin) </code></pre> <p>Note: 'all' is for all media types. You can also use 'screen', 'print', 'projection', etc.</p> <p>Done!</p> <p>As an alternative Django provides a way to add css classes to fieldsets via admin.py.</p> <p>The default admin stylesheet contains 'wide' and 'extrapretty' css classes. They give your admin 'add' and 'change' pages a wide and extrapretty look. This look has wider labels! So if you're lucky setting these is enough and you don't need to add a custom style sheet! :)</p> <p>To set a css class for a fieldset you need to define fieldsets in your ModelAdmin.</p> <p>admin.py:</p> <pre><code>class MyModelAdmin(admin.ModelAdmin): ... fieldsets = ( (None, { 'classes': ('wide', 'extrapretty'), 'fields': ('numInvestments', '...' ) # And all other fields. }), admin.site.register(MyModel, MyModelAdmin) </code></pre> <p>If 'wide', 'extrapretty' is not sufficient, use 'fieldsets' to add a custom css class ('extra_wide') to your fieldset. </p> <p>admin.py:</p> <pre><code> class MyModelAdmin(admin.ModelAdmin): ... fieldsets = ( (None, { 'classes': ('extra_wide', ), 'fields': ('numInvestments', 'other_field_with_wide_label' ) }), (None, { 'fields': ('other_field', '...' ) # All other fields. }), ) class Media: css = { 'all': ('/path/to/my.css',) } admin.site.register(MyModel, MyModelAdmin) </code></pre> <p>my.css:</p> <pre><code>.extra_wide label { width: 300px; } </code></pre> <p>The advantage of fieldsets is that you can use django's default css or create the style sheet only once. The rest is controlled form admin.py. So this is better for multiple occurrences of wide labels.</p> <p>Yet another alternative: Just avoid the problem. </p> <p>1) Make your label shorter and use 'help_text' to explain what your field is about:</p> <p>models.py:</p> <pre><code>numInvestments = models.IntegerField('Your total number of investments') </code></pre> <p>will be:</p> <pre><code>numInvestments = models.IntegerField('Investments', help_text="Your total number of investments.") </code></pre> <p>2) Just make your label shorter and use fieldsets to put a title above the field that explains what this field is about:</p> <p>models.py:</p> <pre><code>numInvestments = models.IntegerField('Number') </code></pre> <p>admin.py:</p> <pre><code> class MyModelAdmin(admin.ModelAdmin): ... fieldsets = ( ('Investments', { 'fields': ('numInvestments', ) }), ) admin.site.register(MyModel, MyModelAdmin) </code></pre> <p>I would use the css atrribute selector if I really need a long label and it is a unique exception. But if there are more long labels, I whould use the fieldset with 'wide' and 'extrapretty' or custom class ('extra_wide'). But most of the time I would try to avoid this problem by using shorter labels, help_text and grouping fields in fieldsets with titles.</p> <p>Did this answer your question? I hope it helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload