【问题标题】:Reverse for 'django_summernote-upload_attachment' not found. 'django_summernote-upload_attachment' is not a valid view function or pattern name找不到“django_summernote-upload_attachment”的反向。 'django_summernote-upload_attachment' 不是有效的视图函数或模式名称
【发布时间】:2022-07-06 14:50:53
【问题描述】:

我尝试添加帖子时一直出现此错误。 它说,

“未找到 'django_summernote-upload_attachment' 的反向。'django_summernote-upload_attachment' 不是有效的视图函数或模式名称。”

我知道这不是添加“path('summernote/', include('django_summernote.urls')”的问题,因为我已经这样做了。

models.py

from django.db import models
from django.contrib.auth.models import User
from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget

CATEGORY = (
    ("Beginner", "Beginner"),
    ("Intermediate", "Intermediate"),
    ("Advanced", "Advanced"),
)

class Post(models.Model):
    title = models.CharField(max_length=300, unique=True)
    slug = models.SlugField(max_length=300, unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='course_posts')
    content = models.TextField()
    # content = models.CharField(widget=SummernoteWidget())
    category = models.CharField(max_length=25, choices=CATEGORY, default="Beginner")

    created_at = models.DateField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return self.title

urls.py

from . import views
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

app_name = 'course_app'

urlpatterns = [
    path('course/', views.PostList.as_view(), name='course'),
    path('course/<slug:slug>/', views.PostDetail.as_view(), name='course_posts'),
    path('summernote/', include('django_summernote.urls')),
    path('editor/', include('django_summernote.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py

from django.shortcuts import render
from django.views import generic
from .models import Post


class PostList(generic.ListView):
    """
    Return all posts that are with status 1 (published) and order from the latest one.
    """
    queryset = Post.objects.order_by('-created_at')
    template_name = 'course.html'


class PostDetail(generic.DetailView):
    model = Post
    template_name = 'course_post.html'


def courses(request):
    return render(request, 'course.html', {'navbar': 'courses'})

我尝试添加一个 jpg 文件作为附件,这很有效。这只是我有错误的“添加帖子”功能。

请帮忙!

【问题讨论】:

    标签: django summernote


    【解决方案1】:

    您的 urlpatterns 变量中似乎不存在名称为 'django_summernote-upload_attachment' 的模式。查看此包中的urls.py 文件(请参阅https://github.com/summernote/django-summernote/blob/main/django_summernote/urls.py),我们可以看到仅当配置的'disable_attachment' 键值为False 时才会添加相应的模式。

    检查您的settings.py 文件并查找SUMMERNOTE_CONFIG 变量。在那里你应该看到'disable_attachment' 键。确保其值设置为False(请参阅https://github.com/summernote/django-summernote#options)。否则,Django 尝试访问的 urlpattern 将不会被注册,您将收到您发布的错误。

    附带说明,请注意urls.py 文件和其中包含的模式,因为您在两个不同的端点中包含相同的模式集:

     path('summernote/', include('django_summernote.urls')), # using include('django_summernote.urls')
     path('editor/', include('django_summernote.urls')) # using include('django_summernote.urls') once again
    

    如果包含的模式具有相同的命名,这可能会导致重定向问题。删除这两种模式中的一种就可以了,只需在其他文件中考虑到它!

    【讨论】:

    • 抱歉回复晚了,但我刚刚修好了?对我来说,问题不在于“disable_attachment”键;我在主应用程序的 urls.py 中添加了“path('summernote/', include('django_summernote.urls'))”,而不是特定应用程序(Post)的 urls.py。这解决了问题。感谢您的意见!
    【解决方案2】:

    这是我的 URL 的样子,它运行良好,但在我遇到同样的错误之前,我通过添加这两个路径 summernoteeditor 来修复它

    urlpatterns = [
        path('', RedirectView.as_view(url='admin/login/', permanent=False), name='/'),
        path('admin/', admin.site.urls),
        path('summernote/', include('django_summernote.urls')),
        path('editor/', include('django_summernote.urls')),
    ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-25
      • 2020-12-01
      • 2019-06-27
      • 2021-05-18
      • 2018-11-19
      • 2019-04-21
      • 2021-07-18
      • 2022-10-04
      相关资源
      最近更新 更多