Zinnia has no content field in Django Admin - python

I have setup Django CMS with Zinnia for the first time. However in the admin area there is no place to enter actual content! See image...
Everything else works. What I'm I doing wrong here?

The problem is that you're using this plugin https://github.com/django-blog-zinnia/cmsplugin-zinnia which reset admin page here if you're set cmsplugin_zinnia.placeholder.EntryPlaceholder as ENTRY_BASE_MODEL.
As you see this code cuts the original fieldset and remove content field:
fieldsets = (
(_('Content'), {'fields': (('title', 'status'), 'image')}),) + \
EntryAdmin.fieldsets[1:]
So I see only one solution is to set another model as ENTRY_BASE_MODEL which you need to create:
from zinnia.models_bases.entry import AbstractEntry
class Entry(AbstractEntry):
pass
And finally set correct settings.py
ENTRY_BASE_MODEL = 'path_to_module.Entry'
I hope this will help you :)
Note
I digged out why the hell they reset fieldset the original admin. This is explained here.

Related

Suit form include on list display in admin

Im trying to use Django Suit's form includes to add a button to the list display of all my subscribers in the admin. In the documentation it says to add this to your admin.py file in the right app.
class SubscriberAdmin(admin.ModelAdmin):
list_display = ('email', 'date')
readonly_fields = ('email', 'date')
def has_add_permission(self, request):
return False
suit_form_includes = (
('admin/suit_includes/suit_csv.html', 'top'),
)
However this only appears when clicking into an instance of an object, and doesn't show up on the admin page that shows the entire list of objects in the database. Is there a way to do this with Django Suit ? I had trouble finding anything on google.
suit form include template:
<button class="btn btn-info">Export to File</button>
Admin instance display (Where its appearing):
Admin list display (Where I want it to appear):
What's doing django-suit, here, is that it is including your HTML snippet in the change_form that is displayed for your model, the change_form being where you can modify your model and save the changes into the database.
Where you want it to appear is the "change_list", aka the place where you can see all of your instances of that model in your database.
To add it your html snippet, you should extend your change_list.html with your own snippet : More info on expanding templates in the official documentation
Good luck !

Django Registration Redux Registration form setting

In Django Registration Redux, there are alternative registration forms that one can use. For Example, one can use RegistrationFormTermsOfService or RegistrationFormUniqueEmail in the registration.forms file. I read the code and figure that the way to do this is by setting a REGISTRATION_FORM variable in the settings.py file. In registration.views we see the following:
REGISTRATION_FORM_PATH = getattr(settings, 'REGISTRATION_FORM','registration.forms.RegistrationForm')
REGISTRATION_FORM = import_string( REGISTRATION_FORM_PATH )
However, when I do set the REGISTRATION_FORM in settings.py to 'registration.forms.RegistrationFormUniqueEmail', I still can't get the form for unique email. Any help will be appreciated.
Okay, so I figured this one out. Basically, in the version 1.1 of django registration redux, this option of setting which registration form in the setting was not available. The code I was looking at from github is the version 1.2 version. So if this happens to you, just install from github.

How to integrate the feature of third party app (django-avatar) in my app?

I developed the user profile setting page. I don't know how to include the change avatar feature in the setting page ?
I use {% include avatar/add.html %} in the userprofile.html . I know it is wrong but I didn't find out any solution. Please help me.
Thank you in advance
Update: I want to change avatar feature on the same setting page. . Thanks
INSTALLED_APPS = (
# ...
'avatar',
)
just put below code in your page
Change your avatar

Django: Remove "view on site" button in the admin User change form

get_absolute_url() method is cool, but in some cases is not needed. django.contrib.auth.models.User has it set by default, this cause my projects to have a broken link in the admin.
How can I prevent that from happening?
In one of my old projects I set a custom template in which I removed the html of the button, it doesn't sound like a good solution that would scale though. Anything better?
If you click on Django 1.7 link, the site will tell you that "Its an insecure version of Django that is no longer supported. Please upgrade to a newer release!"
For Django 1.9, following solution works fine as mentioned in the Django documentation
in myapp/admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
# Disable View on Site link on admin page
site_url = None
This can be done, per model, as of django 1.7.
# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
view_on_site = False
admin.site.register(MyModel,MyModelAdmin)
Instead of monkey patching you can hide the button on the client side using JavaScript. The HTML of the view on site button looks like this:
<li>View on site</li>
If you just hide the anchor tag you will get part of the round button appearing as that is applied on the li tag. Now unfortunately there is no easy way to use css to select that specific li tag since it doesn't have a class, name or id on it. So we can use jquery which gives you more control on your selectors. Put the following in your static folder. For example in the location static/admin/user_change_form.js
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
});
Your admin.py could then look something like this:
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.admin import site
class CustomUserAdmin(UserAdmin):
class Media:
js = ['admin/user_change_form.js']
site.unregister(User)
site.register(User, CustomUserAdmin)
UPDATE
A feature was added in Django 1.7 ModelAdmin.view_on_site which lets you not display the “View on site” link.
I know this is old but I came across it when I had to do the same thing.
A better solution would be to monkey patch it in an accounts/admin.py file when you have accounts in INSTALLED_APPS.
admin.site.unregister(User)
# We don't want a broken View on Site link. Thanks for that, contrib.auth!
del User.get_absolute_url
admin.site.register(User, MyUserAdmin)
Django 2.0 above you can add in default admin
admin.site.site_url = None
Above trick worked for me very well.
As a last resort, I have a monkey_patch app at the bottom of my INSTALLED_APPS which modifies the built in django contrib apps in ways I haven't found better ways to modify such as username length, default admin forms, __unicode__, etc.
Just watch your back when you upgrade django / in general.
from django.contrib.auth.models import User
del User.get_absolute_url
I'm using Django 1.4 and Marwan Alsabbagh's solution worked fine for me. Although, when opening/refreshing User change form there was a short blink. That is because JQuery hides this button only when page is loaded.
To solve this minor issue I used CSS to hide the whole .change-form block. After page is loaded this block's visibility is restored by means of JQuery. So, my code looks like this:
admin.py:
class Media:
js = ['js/admin/user_change_form.js']
css = {'all': ('css/admin/user_change_form.css',)}
...static/css/admin/user_change_form.css
.change-form {
visibility: hidden;
}
...static/js/admin/user_change_form.js
/* Do not show 'View on site' button in User change form */
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
/* restore visibility of the page (hidden in css to avoid blinking) */
$(".change-form").css('visibility', 'visible')
});
use
admin.site.site_url = "your url here"
in url.py of ur main app to modify the "visit site" on django page
and
for "view_on_site" removal
use
view_on_site = False in ur class with display_list for
Inside your app config (apps.py) do this:
class MyAppConfig(AppConfig):
def ready(self):
admin.site.site_url = None
Works in Django 4.0 as well.

Django Admin's "view on site" points to example.com instead of my domain

I added a get_absolute_url function to one of my models.
def get_absolute_url(self):
return '/foo/bar'
The admin site picks it up and adds a "view on site" link to the detail page for that object (when I put a real URL there instead of "/foo/bar").
The problem is instead of going to http://localhost:8000/foo/bar, it goes to http://example.com/foo/bar.
What am I doing wrong?
You have to change default site domain value.
The funniest thing is that "example.com" appears in an obvious place. Yet, I was looking for in in an hour or so.
Just use your admin interface -> Sites -> ... there it is :)
You can change this in /admin/sites if you have admin enabled.
As others have mentioned, this is to do with the default sites framework.
If you're using South for database migrations (probably a good
idea in general), you can use a data migration to avoid having to make this same database change everywhere you deploy your application, along the lines of
from south.v2 import DataMigration
from django.conf import settings
class Migration(DataMigration):
def forwards(self, orm):
Site = orm['sites.Site']
site = Site.objects.get(id=settings.SITE_ID)
site.domain = 'yoursite.com'
site.name = 'yoursite'
site.save()
If you are on newer versions of django. the data migration is like this:
from django.conf import settings
from django.db import migrations
def change_site_name(apps, schema_editor):
Site = apps.get_model('sites', 'Site')
site = Site.objects.get(id=settings.SITE_ID)
site.domain = 'yourdomain.com'
site.name = 'Your Site'
site.save()
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.RunPython(change_site_name),
]
When you have edited a Site instance thought the admin, you need to restart your web server for the change to take effect. I guess this must mean that the database is only read when the web server first starts.

Categories