django filter_horizo​​ntal无法显示

时间:2013-08-10 16:48:16

标签: python django

这是模特:

author=models.ManyToManyField(Author)

的ModelAdmin: filter_horizontal=("author",)

它仍在呈现多选小部件。 我不知道是什么问题。

Django 1.5。

mysite的/ mysite的/ views.py

from django.shortcuts import render_to_response
from django.http import HttpResponse,Http404
import datetime

def hello(request):
    return HttpResponse("Hello world")

def current_time(request):
    now=datetime.datetime.now()
    return render_to_response('current_datetime.html',{'current_date':now})#(template,context) ADDED BY ROBERT

def hours_ahead(request,offset):
    try:
    offset=int(offset)
    except ValueError:
    raise Http404()
    dt=datetime.datetime.now()+datetime.timedelta(hours=offset)
    html='In %d hours, it will be %s.'%(offset,dt)
    return HttpResponse(html)

def display_meta(request):
    values=request.META.items()
    values.sort()
    html=[]
    for k,v in values:
    html.append('<tr><td>%s</td><td>%s</td></tr>'%(k,v))
    return HttpResponse('<table>%s</table>' % '\n'.join(html))

mysite的/ mysite的/ urls.py

from django.conf.urls import patterns, include, url
from mysite.views import *
#from books import views
from books import views

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    ('^hello/$',hello),
    ('^time/$',current_time),
    (r'^time/(\d{1,2})/$',hours_ahead),
    (r'^admin/',include(admin.site.urls)),
    (r'^display_meta/$',display_meta),
    (r'^search/$',views.search),
)

mysite的/书籍/ models.py

from django.db import models
from django.contrib import admin

# Create your models here.

class Publisher(models.Model):
    name=models.CharField(max_length=30)
    address=models.CharField(max_length=50)
    city=models.CharField(max_length=60)
    state_province=models.CharField(max_length=30)
    country=models.CharField(max_length=30)
    website=models.URLField()

    def __unicode__(self):
    return self.name

class Author(models.Model):
    first_name=models.CharField(max_length=30)
    last_name=models.CharField(max_length=30)
    email=models.EmailField(blank=True,verbose_name='e-mail')
    def __unicode__(self):
    return u'%s %s' %(self.first_name,self.last_name)

class AuthorAdmin(admin.ModelAdmin):
    list_display=('first_name','last_name','email')
    search_fields=('first_name','last_name')

class Book(models.Model):
    name=models.CharField(max_length=100)
    author=models.ManyToManyField(Author)
    publisher=models.ForeignKey(Publisher)
    publication_date=models.DateField(null=True,blank=True,verbose_name='Publication Date')
    def __unicode__(self):
    return u'%s' % self.name

class BookAdmin(admin.ModelAdmin):
    list_display=('name','publisher','publication_date')
    list_filter=('publication_date',)
    ordering=('-publication_date',)
    date_hierachy='publication_date'
#fields=('name','author','publisher','publication_date')
    filter_horizontal=('author',)

admin.py

from django.contrib import admin
from books.models import *

admin.site.register(Publisher)
admin.site.register(Author,AuthorAdmin)
admin.site.register(Book,BookAdmin)

1 个答案:

答案 0 :(得分:2)

我觉得回答已经太晚了;然而,我在阅读django-core书时遇到了同样的问题,我认为这可能是你问题的根源:

只需在隐身模式或其他浏览器中打开管理面板,您也可以通过从浏览器中删除缓存内容来解决问题。我认为问题可能源于javascript未完成加载,这是您所做的更改之前的第一次。然而;以隐身私密模式打开它解决了我的问题。