3

我想在我的 Django 2.1.1 站点 django-filebrowser-no-grappelli 上有。我已遵循此指示,但在程序结束时,当我重新启动服务器时,出现此错误:

header_image = models.FileBrowseField("Image", max_length=200, directory="images/", extensions=[".jpg"], blank=True) AttributeError: module 'django.db.models' 没有属性 'FileBrowseField'

这是我的项目的文件:

模型.PY

from django.db import models
from django.urls import reverse
from tinymce import HTMLField
from filebrowser.fields import FileBrowseField

class Post(models.Model):
    """definizione delle caratteristiche di un post"""
    title = models.CharField(max_length=70, help_text="Write post title here. The title must be have max 70 characters", verbose_name="Titolo", unique=True)
    short_description = models.TextField(max_length=200, help_text="Write a post short description here. The description must be have max 200 characters", verbose_name="Breve descrizione dell'articolo")
    contents = HTMLField(help_text="Write your post here", verbose_name="Contenuti")
    publishing_date = models.DateTimeField(verbose_name="Data di pubblicazione")
    updating_date = models.DateTimeField(auto_now=True, verbose_name="Data di aggiornamento")
    header_image = models.FileBrowseField("Image", max_length=200, directory="images/", extensions=[".jpg"], blank=True)
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("singlearticleView", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Articolo"
        verbose_name_plural = "Articoli"

意见.PY

from django.shortcuts import render
from .models import Post
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

class SingleArticle(DetailView):
    model = Post
    template_name = "single_article.html"


class ListArticles(ListView):
    model = Post
    template_name = "list_articles.html"

    def get_queryset(self):
        return Post.objects.order_by('-publishing_date')

URLS.PY

from django.urls import path
from .views import ListArticles, SingleArticle

urlpatterns = [
    path("", ListArticles.as_view(), name="listarticlesView"),
    path("<slug:slug>/", SingleArticle.as_view(), name="singlearticleView"),
]

管理员

from django.contrib import admin
from .models import Post

class PostAdmin(admin.ModelAdmin):
    """creazione delle caratteristiche dei post leggibili nel pannello di amministrazione"""
    list_display = ["title", "publishing_date", "updating_date", "id"]
    list_filter = ["publishing_date"]
    search_fields = ["title", "short_description", "contents"]
    prepopulated_fields = {"slug": ("title",)}
    fieldsets = [
                (None, {"fields": ["title", "slug"]}),
                ("Contenuti", {"fields": ["short_description", "contents"]}),
                ("Riferimenti", {"fields": ["publishing_date"]}),
            ]

    class Meta:
        model = Post

admin.site.register(Post, PostAdmin)

URLS.PY 项目

from django.contrib import admin
from django.urls import path, include
from filebrowser.sites import site

urlpatterns = [
    path('admin/filebrowser/', site.urls),
    path('grappelli/', include('grappelli.urls')),
    path('admin/', admin.site.urls),
    path('', include('app4test.urls')),
    path('tinymce/', include('tinymce.urls')),
]

我已经开始了这一切,因为我将使用 TinyMCE,并且需要一个文件浏览器应用程序。当我停用 models.py 中的字符串 header_image 时,项目运行良好,但显然,当我尝试上传图像时出现错误。

我在哪里做错了?

4

1 回答 1

3

您的导入是这样完成的:

from filebrowser.fields import FileBrowseField

但是您正在尝试通过以下方式使用该字段:

header_image = models.FileBrowseField(...)

它不是models包装的一部分,只需在没有models.部分的情况下进行:

header_image = FileBrowseField(...)
于 2018-09-22T08:45:57.093 回答