【问题标题】:Module 'django.db.models' has no attribute 'FileBrowseField'模块“django.db.models”没有属性“FileBrowseField”
【发布时间】:2019-02-26 12:30:45
【问题描述】:

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

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

这是我项目的文件:

MODELS.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"

VIEWS.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"),
]

ADMIN.PY

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 时,项目运行良好,但显然,当我尝试上传图片时出现错误。

我哪里做错了?

【问题讨论】:

    标签: django python-3.x django-file-upload django-filebrowser django-2.1


    【解决方案1】:

    您的导入是这样完成的:

    from filebrowser.fields import FileBrowseField
    

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

    header_image = models.FileBrowseField(...)
    

    它不是models 包的一部分,只需不带models. 部分即可:

    header_image = FileBrowseField(...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 2019-07-20
      • 2019-09-30
      • 2021-03-12
      相关资源
      最近更新 更多