I try to add my custom javascript to some change_form templates.
Before having installed Grappelli, I could do that by extending
change_form.html of admin and creating sub folders under the templates
with my module names.
For example my tree hireachy was like :
+templates
++admin_copies
+++change_form.html (I have added extra js blocks)
++admin
+++employer
++++employer
+++++change_form.html
++++employer_new
+++++change_form.html
As you understand I have a model named "Employer". I can add my custom
js methods under the change_form.html files.
However, this kind of inheritance cause javascript/breadcrumbs
problems with Grappelli.
How can I add simply my custom javascript functions for each model
separetly ?
Create a subclass of ModelAdmin, and define the custom js in its Media class.
admin.py, in the same folder as the models.py that has Employer:
from django.contrib import admin
from models import Employer
class EmployerAdmin(admin.ModelAdmin):
class Media:
js = ("js/custom.js",) # This paths are appended to your MEDIA_URL setting
admin.site.register(Employer, EmployerAdmin)
Read more here
Related
In Admin for InlineForm there is a link that goes to the website, be default the text is (View on site).
I want to add a similar link that goes to that model(that is inline) Detail Edit Page.
Insert a JavaScript script in one of the Edit/Details Model Page in Admin
How can this be done ?
Adding JS and CSS to a models list_view and form_view pages are easy.
Write a class Media inside the admin class for the respective model.
admin.py
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("css/mycss.css",)
}
js = ("js/my_js.js",)
Place the javascript file inside /yourapp/static/yourapp/js/my_js.js
and css inside /yourapp/static/yourapp/css/mycss.css
I want to implement in Django Admin a jquery plugin that "adjust" and image(http://guillotine.js.org/), them get the coordinates with ImageKit and save the new image.
I need some tutorials and advises how to do it.
I have no tutorials, but can give you advice.
You can customize your admin model with custom css and js, by Media class, like so:
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("my_styles.css",)
}
js = ("my_code.js",)
You can look in dev tools, how Django chose names id's and classes for elements in page and also check the docs.
Admin docs
I've installed a django reusable app (Django-Userena) and would like to overwrite the given models.py file.
I have created an app named 'accounts' that calls from Django-Userena. In my 'accounts' app, I have this models.py file that has a class MyProfile that inherits from Django-Userena class UserenaBaseProfile - class MyProfile(UserenaBaseProfile)
In the UserenaBaseProfile class, there is the following code:
privacy = models.CharField(_('privacy'),
max_length=15,
choices=PRIVACY_CHOICES,
default=userena_settings.USERENA_DEFAULT_PRIVACY,
help_text = _('Designates who can view your profile.'))
I would like to extend privacy with an extra value with 'editable=False,' as I do not want this field to be displayed in the auto-generated form.
I tried several ways like calling privacy again in the MyProfile inherited model with the new settings but I am only made aware of Django's "Field name "hiding" is not permitted" (https://docs.djangoproject.com/en/1.4/topics/db/models/#field-name-hiding-is-not-permitted)
My current solution is to simply include the whole UserenaBaseProfile class in my 'accounts' app models.py before calling class MyProfile(UserenaBaseProfile) below.
This does not look like an elegant solution to me. How do you guys go about overriding the models.py file in the reusable app?
Thank you very much.
In my opinion it could be done in two ways:
Make a fork of Django-Userena with your modified model and you use yours.
Make a wrapper of Django-Userena with your models.py and use your wrapper app.
For the urls.py/views.py you could just put:
#Your wrapper views:
from django-userena.views import *
#your wrapper urls:
from django-userena.urls import *
Here are your models:
#your MODIFIED model:
from django-userena.models import *
# then put you new UserenaBaseProfile
class UserenaBaseProfile(models.Model):
#copy the model fields
...
privacy = models.CharField(_('privacy'),
max_length=15,
choices=PRIVACY_CHOICES,
default=userena_settings.USERENA_DEFAULT_PRIVACY,
help_text = _('Designates who can view your profile.'))
Then you could use your custom app in your project.
If you want to customise templates, create a templates directory in your project and put there your modified template files keeping their original names, so the django template-loader could find yours first (it depends how template-loaders have been configured in your settings.py)
Please suggest the best way to add tinymce to django admin area. Is it possible to add it by extending /admin/change_form.html in my template directory ?
The best way in my opinion is django-tinymce.
Its awesome and super easy to integrate into your project, plus you can add django-filebrowser in easily for image uploading.
django-tinymce is the way to go. You can use pip to install it. You use it on model fields like so:
from tinymce import models as tinymce_models
class Foo(models.Model):
description = tinymce_models.HTMLField(blank=True, null=True, help_text="You can use HTML markup - be careful!")
If you are using South for DB migrations you need to help it out with this line:
add_introspection_rules([], ["^tinymce.models.HTMLField"])
Works like a charm!
Put the tiny_mce.js library somehwere in your media folder. For example in js/tiny_mce/
Then (for django 1.2) you need to create a custom model admin in your_app/admin.py. Add a class Media with js attribute to it. Example:
from django.contrib import admin
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ('js/tiny_mce/tiny_mce.js',
'js/admin/textareas.js',)
admin.site.register(MyModel, MyModelAdmin)
In media/js/admin/textareas.js you can add your call to tinyMCE.init. Example:
tinyMCE.init({
mode : "textareas",
theme : "advanced"
});
That's it. Javascript is included automatically. No need to overwrite admin templates.
Note: One thing I forget to mention that in this case it only applies to the admin for MyModel. If you need the same functionality for all yout model's, simply register this custom ModelAdmin to them or add Media classes to exising ModelAdmin classes.
I got tinyMCE working for flat pages by copying the change_form.html file and embedding a script element.
I followed the directions here but still have issues. I added this snippet to my Entry model:
class Entry( models.Model ):
class Admin:
# various admin options are here
js = (
'/tiny_mce/tiny_mce.js',
'/appmedia/admin/js/textareas.js',
)
When I view source in my add entry admin page ( after restarting Apache2 ) I don't see the above referenced js files anywhere.
The directions you linked to are very old, and in particular date back to before the 'newforms-admin' changes were made. You need to add the js tuple to your admin class's inner Media class, as described here.
Ended up resorting to django-tinymce, had to modify my app's admin.py and define a class for the AdminForm, override the form attribute with my newly created class, and tinyMCE finally showed up.