I have a form in my app that has content attribute and uses tinymce like so:
from tinymce.models import HTMLField
class SomeModel(models.Model):
content = HTMLField('Content', null=True)
I also set up my tinymce in the installed apps and have the registered its url.
In my app it works fine and the user can edit and enter his\her content using tinymce. but when I register this model to the admin, all fields appear fine except the content which does not appear to have any way to input (and because its a required field, this means that I cant enter new items through the admin).
this looks like this in the admin:
how can I make tinymce also available in the admin screen?
bonus question: is there a safe way to use tinymce while letting the users use tinymce (currently im using form | safe which im guessing isn't really safe.
Create ModelForm
class BlogForm(ModelForm):
body = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
Admin.py
from models import Blog
from forms import BlogForm
class BlogAdmin(ModelAdmin):
form = BlogForm()
Related
I have a model that I only want to use one row of its table. So, on admin, I would like to remove the list and add pages, and only edit the existing object. The model is this:
from django.db import models
class Banner(models.Model):
pass
class BannerImg(models.Model):
img = models.ImageField(upload_to="banners")
banner = models.ForeignKey(Banner, on_delete=models.CASCADE)
Basically, the Banner(pk=1) will be loaded by the frontend to display a landing page hero slider. I want multiple images, but also want them to be on the same admin form, so one could order, add or remove images from the same place. Of course having to Banner objects, wouldn't make sense in this case.
I can use inline fields to do the form, but how can I achieve the pages functionality (going directly to edit)?
Thanks!
Accordion to documentation.
from django.contrib import admin
class BannerImgInline(admin.TabularInline):
model = BannerImg
class BannerAdmin(admin.ModelAdmin):
inlines = [
BannerImgInline,
]
i am have enabled everything needed to work with spatial data at the database and django setting level, my profile model has a default_location field that is a PointField. as shown below.
from django.contrib.gis import models
class Profile(models.Model):
...
default_location = models.PointField()
i registered the profile model as an inline to be viewed and edited from within a User model (one-to-one relationship between user and profile).code shown below
class ProfileInline(StackedInline):
model = models.Profile
class NewUserAdmin(admin.GISModelAdmin):
gis_widget = OSMWidget
inlines = [ProfileInline]
admin.site.unregister(models.User)
admin.site.register(models.User, NewUserAdmin)
however i keep getting a openlayer map in my django admin page
please can anyone suggest a fix to this. i need open street map because of it detailed street feature.
You can use the django-leaflet package. By default an OpenStreetMap is displayed, and it also has better tools and interface.
After installing you need add leaflet to INSTALLED_APPS in settings.py.
Then you use LeafletGeoAdmin in your ModelAdmin in admin.py.
You can add some customizations by adding this to your settings.py:
LEAFLET_CONFIG = {
'DEFAULT_CENTER': (39.694819, -8.130229),
'DEFAULT_ZOOM': 6,
'MAX_ZOOM': 20,
'MIN_ZOOM':3,
'SCALE': 'both'
}
More information here: https://django-leaflet.readthedocs.io/
Here's a piece of Django admin interface's instance edition form:
How should I change the underlying admin.ModelAdmin instance to make it contain an URL, like this?
Django makes this easy. Subclass ModelAdmin, add a custom method and then tell the Admin how to use it. Here's a sample admin.py:
from django.contrib import admin
from .models import Vendor
class VendorAdmin(admin.ModelAdmin):
readonly_fields = ['example_link']
def example_link(self, obj):
return 'link text'.format(obj.get_link()) # however you generate the link
example_link.allow_tags = True
admin.site.register(Vendor, VendorAdmin)
Here's the documentation that furthers explains readonly_fields, customizing the form label text with short_description, ordering, and how you can put this custom url method on the Model or ModelAdmin.
I have a 'project' model. Each project has a 'gallery' and each gallery has 'photos'.
class Project:
gallery = ForeignKey(Gallery)
class Gallery:
photos = ManyToManyField(Photo)
class Photo:
image = ImageField(...)
I want to let my users edit the gallery and the project on the same page. Could you tell me what components I need to make this happen? Like which type of form I should use and what technique to use when I process the form with the uploaded images and all?
What to take into account is that I want to show the photos the user is editing with the html img-tag as well as file-tag to let him replace the photo. I don't want django's default m2m-widget which is just a multiselect-list.
Could you help me figure this out, because I simply can't. Been stuck here for three days :)
You can modify your Gallery Admin form using Project as a admin.TabularInline.
Like this:
admin.py
# -*- encoding: utf-8 -*-
from models import Project, Gallery, Photo
from django.contrib import admin
class ProjectInline(admin.TabularInline)
model = Project
class GalleryAdmin(admin.ModelAdmin):
inlines = [ProjectInline]
admin.site.register(Gallery, GalleryAdmin)
I didn't want to use the built in admin module. But I used the formset factory by django. It will let me provide a queryset to the formset (i.e the photos in the gallery). Then I had to provide a small customized model formset class, and then i the view I pretty much had to process the form manually in order to link it correct to the gallery and such..
In my flatpage admin change list page, mysite.com/admin/flatpages/flatpage/, I can see the fields:
URL
Title
Is there a way to also show the field Site? I associate my flatpages to specific sites. The bad way to do it is by going to the actual Flatpage admin source django/contrib/flatpages/admin.py and create a method which will display sites for a Flatpage on the change list page.
I am basically looking for a way to overwrite a django.contrib application on the admin side.
You don't need to edit flatpages/admin.py. Instead, create a CustomFlatPageAdmin that inherits from the default FlatPageAdmin.
You might want to create a customflatpage app for the following admin.py file, or perhaps you already have a utilities app that you can add it to.
#admin.py
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin
def get_sites(obj):
'returns a list of site names for a FlatPage object'
return ", ".join((site.name for site in obj.sites.all()))
get_sites.short_description = 'Sites'
class CustomFlatPageAdmin(FlatPageAdmin):
list_display = ('title', 'url', get_sites)
#unregister the default FlatPage admin and register CustomFlatPageAdmin.
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, CustomFlatPageAdmin)