I'm looking for a way to lowercase the first letter of a model in my django admin site.
i.e.:
model verbose name is "agent-1.0.0" is shown as "Agent-1.0.0" on the dashboard,
simple but IDK
grappelli trick will also work for me.
django - 1.7.1
also - need this only for one app models group - not all of my dashboard should be lowercase...
so, overriding the index.html is not so efficient
The capitalization is hard-coded in the template, same for the templates in Grappelli.
You can use catavaran's suggestion, but this will transform every model name. Overriding the template is a huge pain in the ass to maintain for something this small.
The only workable solution I can think of is to bypass the capfirst filter with a space:
class Meta:
verbose_name = " agent-1.0.0"
As capfirst only forcibly capitalizes the first character, nothing will happen if the first character is not a letter.
Model name passed to template as capfirst(model._meta.verbose_name_plural) so you have to lowercase it in the admin/index.html tempate or via CSS. Imho CSS option is simpler:
div.module tr[class^=model-] th {
text-transform: lowercase;
}
If you want lowercase only some models (for example User) then change CSS selector to this:
div.module tr.model-user th {
text-transform: lowercase;
}
With Grapelli you could create a custom Dashboard by running:
python manage.py customdashboard
and setting GRAPPELLI_INDEX_DASHBOARD on your settings to your custom class.
You can make this custom class extend from the Dashboard class that grappelli offers and override it to your needs. Look especially at the ModelList class, where you can specify the title you want for the model.
There is a CSS-way for those who don't want to override Django admin classes. Override and extend templates/admin/base_site.html template as follows:
{% extends "admin/base_site.html" %}
{% block extrahead %}
<style>
h1.model-title {text-transform: lowercase;}
h1.model-title:first-letter {text-transform: uppercase;}
</style>
{% endblock %}
{% block content_title %}
{% if title %}<h1 class="model-title">{{ title }}</h1>{% endif %}
{% endblock %}
This will make only first letter of each content_title uppercase.
You can use the same way to lowercase model name in admin tables as well as sidebar. However, I'd like to point that by tacit agreement model's verbose_name as well as verbose_name_plural shouldn't be capitalized. This will save you a lot of overrides in your project, like I provided above to normalize change_list header.
Related
In the Django CMS there's the {% placeholder 'content' %}. I tried to use it on a non-django-cms page, i.e., a detail-view page that comes from an apphook. However, when I switch to the structure view in the detail-view page and the placeholder does not seem to reflect. Is that's how it's supposed to work or is there a problem with my code? If it's how it's supposed to work is there a way to make placeholder appear in the page?
You can't use {% placeholder outside of CMS pages.
If you're on one of these pages, you can use a static placeholder. These will show the same content on any page where a static placeholder with the same name exists. So a good example of these is a footer, or header where you'd want it to be the same on all pages;
{% static_placeholder "footer" %}
Another thing you can use, good for your example of a detail page in an apphook, is a PlaceholderField on your models.
Take this example;
from django.db import models
from cms.models.fields import PlaceholderField
class Category(models.Model):
name = models.CharField(max_length=20)
description = PlaceholderField('category_description')
In your template you can then render this placeholder and it'll behave like a standard placeholder on a cms page;
{% load cms_tags %}
{% render_placeholder category_instance.description language 'en' %}
You can find docs for PlaceholderField here
I have one app in my django project.
I created seo.py file and add:
from rollyourown import seo
class MyMetadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
class HelpText:
title = "This will appear in the window/tab name, as well as in search results."
keywords = "A comma separated list of words or phrases that describe the content"
description = "This will appear in the description"
class Meta:
seo_views = ('app_name', )
in my base.html in head I added:
{% load seo %}
{% get_metadata %}
but nothing is displayed. What is wrong? (Of course, I added data in the admin panel - My metadatas (View))
Loading a template tag library will not work if you haven't put that application in your INSTALLED_APPS variable in settings.py. Have you put rollyourown.seo in there?
The paths you need to set up should be in the format '/pagename/'. For your homepage you just need '/'.
#ringfirebug, probably you already solved your issue, but I will answer.
You don't need to set Path for each page. Use Model, Model Instance or a view. From your configuration I see that you have still a default view in seo_views. It should look like this to start working:
class Meta:
seo_views = ('your_app_name.view_name',)
seo_models = ('your_model_1', 'your_model_2',)
So, when creating a new Metadata entry in Models or Views you will be able to see these models and / or views in the select box.
Next, in your template, if it still doesn't work, you have to get seo data exactly for this object,
this worked for me.
{% load seo %}
{% get_metadata for obj as metadata %}
{% metadata %}
Here 'obj' is the object, for example you product that you path to the template through your view by RequestContext.
Let me know if you managed to solve it.
By the way, if you already have managed to use external data in your metatags, like "Best {{ product.name }} of the year", let me know how.
I am working on creating a web portal and I want to offer the users the feature of making changes to there profile/dashboard like changed background. etc.
Can anybody please guide me to an efficient approach to achieve this?
Thanks
This is trickery that has more to do with css and javascript than with django templates.
The only thing that is django related here is the actual storing of these preferences.
e.g. the filepaths of the actual background images.
After that you will do something similar to what is described in this answer:
how to change html background dynamically
EDIT
I don't see why you need different directories for every user. Django templates
give you more than enough power to do what you want.
For example, let's say that each user can upload his own background picture. Also
I assume that you follow this popular django pattern for storing additional information
about your users. https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
So we have this UserProfile model:
class UserProfile(models.Model):
CHOICES = (
('vertical', 'Vertical'),
('horizontal', 'Horizontal'),
)
user = models.OneToOneField(User)
background_image = models.ImageField(upload_to='images')
dashboard_layout = models.CharField(max_length=10, choices=CHOICES)
You can pass this extra information to your javascript context(either with Ajax or without)
and then change the background image for every individual user.
Also we could do special layout at the template level like this:
{% extends "base.html" %}
{% block main_body %}
{% if request.user.get_profile.dashboard_layout == 'vertical' %}
{% include "layouts/vertical.html" %}
{% else %}
{% include "layouts/horizontal.html" %}
{% endif %}
{% endblock main_body %}
Kindly help me How can i customize the text&charfield(length,breadth,font,colour) without using templates for the following code
class Record(models.Model):
Name = models.CharField(max_length=200,blank=True,null=True,help_text="Employee Name")
Empid = models.CharField(max_length=300,blank=True,null=True,help_text="Employee ID")
Salary = models.IntegerField(blank=True,null=True)
Bonus = models.IntegerField(blank=True,null=True)
In your admin.py:
class RecordAdmin(admin.ModelAdmin):
# your stuff...
class Media:
css = {
"all": ("my_styles.css",)
}
Documentation for class Media.
Now you should be able to override what you want in the my_styles.css file.
If you want a more general solution (not only for Record model but for many models), a good way to do it is to extend the base_site.html template to add your own CSS file to all admin pages.
{% block blockbots %}
<link rel="stylesheet" type="text/css" href="/media/css/admin_base.css" />
{{ block.super }}
{% endblock %}
I put it in blockbots instead of extrastyle to be sure it will be at the end so it will override all other stylesheets.
Documentation on overriding/extending admin templates.
It is not possible to modify the visual elements of the Django admin from the model. This would totally defeat the purpose of an MVC framework! Separation of concerns dictates that these changes would be done in the template.
If you want to change the text size you'll have to modify the stylesheets for the admin.
Maybe you need to create Forms from your Models and to customize them, here is the oficial doc.
Use widgets to customize with HTML attributes
I'd like to change the column widths in the list display of the Django admin.
Is it possible somehow to add a CSS classname to a column?
I'd preferably not overwrite the entire template to accomplish this.
In Django >= 1.6, CSS classes are included with list_display output:
"The field names in list_display will also appear as CSS classes in the HTML output, in the form of column-<field_name> on each <th> element. This can be used to set column widths in a CSS file."
While this feature is implemented in vers1.6 as StvnW said, for earlier versions you can do the following:
In admin.py:
class MyModelAdmin(admin.ModelAdmin):
# your code here
# specify a stylesheet just for the list view
class Media:
css = {'all': ('css/mymodel_list.css')}
In mymodel_list.css:
/* replace '5' with the column desired */
table#result_list td:nth-child(5) {
width: 15em;
}
Specifying table#result_list will apply this stylesheet only to the list view and won't affect the normal admin page for this model. Also, note that while django uses th for the first column of the model, it still counts for a td child.
There is an open ticket that addresses the need for specifying css classes for table columns in the change_list view.
That said ... in the description for the ticket there's a snippet that injects a custom stylesheet in your change_list-template:
{% extends "admin/change_list.html" %}
{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="/css/poll_admin_changelist.css" />
{% endblock extrastyle %}
So you don't override the whole template, only the part (extrastyle) you need.
Now you could inject your own stylesheet and for example style your columns using the :nth-child-selector
Another option would be to wrap your specific fields in html which can be done using the list_display option. Here you could define a width or class for a wrapped element.
This does only makes sense though, if you want to control the width of limited set of fields