Improving the Search Function with Keywords
On page 34 when defining your first model class, remove the Admin class to replace it with new a separate newforms admin file (hereafter just admin). If I've ignored code that works just fine I just use ... to show this.class SearchKeyword(models.Model):
...
#REMOVE THIS
class Admin:
pass
Instead of the basic class Admin you create an admin.py file in the search folder. The simplest possible admin configuration is:
from django.contrib import admin from cms.search.models import SearchKeyword admin.site.register(SearchKeyword)Registering the SearchKeyword allows it to be autodiscovered and included on the admin site. On p36 the SearchKeyword model admin is improved. Firstly remove core = True, the admin now uses a checkbox instead so this is not used any more. Then create an admin.py in the cms folder.
from django.contrib import admin
from cms.search.models import SearchKeyword
from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin
class SearchKeywordInline(admin.StackedInline):
model = SearchKeyword
max_num = 3
extra = 1
class MyFlatPageAdmin(FlatPageAdmin):
inlines = [
SearchKeywordInline,
]
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, MyFlatPageAdmin)
The custom MyFlatPageAdmin inherits and adds the inline SearchKeyword form. To register your custom FlatPage admin you need to unregister the original first. Finally you can delete the search.admin.py file if you only want the inline form.
Caution - Improperly Configured Admin
While writing this post I got myself in a knot when I got this error:ImproperlyConfigured at /admin/
Error while importing URLconf 'cms.urls': 'type' object is not iterable
Clearly this unhelpful error message is causing all sorts of grief as people migrate code to the new admin classes, but I'm sure relief won't be too far off. For the purposes of learning it's actually not to difficult to resolve once you know what you're meant to be looking for.
The first step to resolving the Improperly Configured error would be to comment out your admin.autodiscover() in urls.py. If the error goes away (though admin won't function correctly) then it's an admin class causing the problem. Uncomment it again then basically find the misbehaving admin class by commenting out the admin.site.register(FooAdminClass) functions in each admin.py until you find the one that is misbehaving.
As it was I had tried to register the SearchKeywordInline StackedInline admin class in it's own right instead of just leaving it as part of the custom FlatPagesAdmin class.
All done with the basics of the new admin? Have a read of this blog post and screencast from oebfare on migrating to the newforms admin.
8 comments:
Hi Brett,
that was really helpfull. Many thanks. When do you think you might have the next chapter done?
best regards,
St.john
A couple of things I ran into:
Make sure you have 'cms' in settings.py | INSTALLED_APPS, or django won't see the admin.py you create there;
I had to stop/restart the server for the changes to take.
I've been following your blog. Reading the chapter first, then your comments on it. I would have benefited greatly having read your comments first, or at least during the chapter. As it was, I searched all over the net found Jason Broyles' site and from that found Tim Huegdon's site. From there I read the 1.0 porting guide on Django's documentation and then glanced over the documentation on the admin site. After all that I got everything in Chapter 3 working well. But next time, if I have any trouble I'll read your blog first.
You can put all the admin classes in search/admin.py. That way you don't need a cms/admin.py and so don't need to add cms to the installed apps in settings. I think this way is a bit better.
I get a syntax error from the urls.py code on page 23. It seems line 9 miss a round bracket:
(r'^tiny_mce/ [...] '/path/to/tiny-mce' },
should be
(r'^tiny_mce/ [...] '/path/to/tiny-mce' }),
There is apparently a typo in page 25 where is said src="/tiny_mce/tiny_mce.js". It should be src="/tiny_mce.js" to comply with the urls.py instructions. I've seen numerous confustions about this and think worth mentioning.
Apart from that, installing tinymce have buged me for a week or so. I even remove django 1.2.3 and installed 1.1.2 hoping to resolve version inconsistencies, and copied the whole code from online sample, but no joy! Tinymce just fails to show up. Any clues is much appreicates :(
@minux The first thing you should do is make sure your tinymce javascript is being served by Django by testing the link to the actual javascript file. That's the most common problem.
The second problem that people run into is a mistake in the TinyMCE.init function. You can usually tell if that is the problem with the Firebug plugin.
Start with the simplest installation which is basically just following the instructions on <a href="http://wiki.moxiecode.com/index.php/TinyMCE:Installation>the TinyMCE site</a>, and using the flatpage changeform template as your first test.
Post a Comment