gpt4 book ai didi

django - 模块 'django.db.models' 没有属性 'FileBrowseField'

转载 作者:行者123 更新时间:2023-12-02 08:57:37 25 4
gpt4 key购买 nike

我想在我的 Django 2.1.1 网站上安装 django-filebrowser-no-grappelli。我关注了this indications但在该过程结束时,当我重新启动服务器时,出现此错误:

header_image = models.FileBrowseField("Image", max_length=200, directory="images/", extensions=[".jpg"], blank=True) AttributeError: module 'django.db.models' has no attribute '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"

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

我哪里出错了?

最佳答案

您的导入是这样完成的:

from filebrowser.fields import FileBrowseField

但您尝试按以下方式使用该字段:

header_image = models.FileBrowseField(...)

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

header_image = FileBrowseField(...)

关于django - 模块 'django.db.models' 没有属性 'FileBrowseField',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52454870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com