我一直试图在我的django-admin页面上为博客帖子实现django pagedown功能
中的文档进行操作这就是我做的事情
models.py
class Entry(models.Model):
title = models.CharField(max_length=200)
post_type = models.CharField(max_length=50,choices= (
('Mobiles', 'Mobiles'),
('Laptops', 'Laptops'),
('Laptop_Accesories', 'Laptop_Accesories'),
('Tablets', 'Tablets'),
('Cameras', 'Cameras'),
('Others', 'Others'),
), default='Others')
author = models.CharField(max_length=30, blank=False)
description = models.TextField(max_length=150, blank=False, default="")
body = models.TextField()
slug = models.SlugField(max_length = 200, unique = True)
publish = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now_add=True)
forms.py
from django import forms
from pagedown.widgets import AdminPagedownWidget
from blog import models
from blog.models import Entry
class EntryForm(forms.ModelForm):
description = forms.TextField(widget=AdminPagedownWidget())
class Meta:
model = Entry
fields = "__all__"
admin.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from . import models
from django.contrib import admin
class EntryAdmin(admin.ModelAdmin):
list_display=("title","created")
prepopulated_fields = {"slug" : ("title",)}
class EAdmin(admin.ModelAdmin):
form = EntryForm
# Register your models here.
admin.site.register(models.Entry, EntryAdmin)
我收到错误
description = forms.TextField(widget = AdminPagedownWidget()) AttributeError:'module'对象没有属性'TextField'
答案 0 :(得分:0)
from django.contrib import admin
from .models import new,new_by_viewer
from pagedown.widgets import AdminPagedownWidget
from django.contrib import admin
class story(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': AdminPagedownWidget },
}
admin.site.register(new,story)
admin.site.register(new_by_viewer)
尝试这样,这对我来说很好
答案 1 :(得分:0)
像这样更改您的admin.py
from django.contrib import admin
from .models import Entry
from .forms import EntryForm
@admin.register(Entry)
class EntryAdmin(admin.ModelAdmin):
form = EntryForm
list_display=("title","created")
prepopulated_fields = {"slug" : ("title",)}