xadmin icon indicating copy to clipboard operation
xadmin copied to clipboard

使用自定义菜单 get_model_url后出现NoReverseMatch错误

Open Yuchener opened this issue 6 years ago • 2 comments

问题描述

在使用xadmin使需要自定义菜单,因此重写了get_site_menu方法,编辑器没有报错,但是访问后台时直接报错NoReverseMatch at /admin/

问题出现的环境背景及自己尝试过哪些方法

django 2.0 python3.6 xadmin是在官方github上直接拷贝的最新版

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码) urls.py

from django.urls import path, include, re_path
import DjangoUeditor
import xadmin
app_name = 'website'
urlpatterns = [
    path('admin/', xadmin.site.urls),
    path('ueditor/', include('DjangoUeditor.urls')),
]

adminx.py

import xadmin
from .models import Plant, User, Animal, Islands, MessageBoard, Encyclopedias
from xadmin import views
from django.contrib.auth.models import Group, Permission

class BaseSetting(object):
    """X admin的全局配置设置"""
    # 主题功能开启
    enable_themes = True
    use_bootswatch = True


class GlobalSettings(object):
    """xadmin 全局配置参数信息设置"""
    site_title = "海洋科普管理系统"
    site_footer = "毕业设计网站"
    def get_site_menu(self):
        return (
            {'title': '用户管理', 'menus': (
                {'title': '用户信息', 'url': self.get_model_url(User, 'changelist')},
            )},
            {'title': '植物管理', 'menus': (
                {'title': '植物信息', 'url': self.get_model_url(Plant, 'changelist')},
            )},
            {'title': '动物管理', 'menus': (
                {'title': '动物信息', 'url': self.get_model_url(Animal, 'changelist')},
            )},
            {'title': '岛屿管理', 'menus': (
                {'title': '岛屿信息', 'url': self.get_model_url(Islands, 'changelist')},
            )},
        )




class PlantAdmin(object):
    style_fields = {"plantDetail": "ueditor"}
    pass
class BakeAdmin(object):
    pass

class UserAdmin(object):
    pass



xadmin.site.register(views.CommAdminView, GlobalSettings)

xadmin.site.register(Encyclopedias,BakeAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)

models.py

from django.db import models
from datetime import datetime
from django.db import models
from DjangoUeditor.models import UEditorField
# Create your models here.


class User(models.Model):
    GENDER_CHOICES = (
        ("male", u"男"),
        ("female", u"女")
    )
    username = models.CharField(max_length=50, verbose_name=u"用户名")
    niCheng = models.CharField(max_length=50, verbose_name=u"昵称")
    gender = models.CharField(
        max_length=6,
        verbose_name=u"性别",
        choices=GENDER_CHOICES,
        default="female")
    phone = models.CharField(max_length=300, verbose_name=u"联系手机")
    register_time = models.DateTimeField(default=datetime.now, verbose_name=u"注册时间")

    class Meta:
        verbose_name = u"用户信息"
        verbose_name_plural = verbose_name


class Plant(models.Model):
    GENDER_CHOICES = (
        ("hongzhao", u"红藻门"),
        ("lvzhao", u"绿藻门"),
        ("hezhao", u"褐藻门"),
        ("beizizhiwu", u"被子植物")
    )
    plantName = models.CharField(max_length=50, verbose_name=u"植物标题")
    plantImage = models.ImageField(
        upload_to="image/%Y/%m",
        max_length=100,
        verbose_name=u"植物图片"
    )
    plantList = models.CharField(
        max_length=20,
        verbose_name=u"植物类别",
        choices=GENDER_CHOICES,
        default="hongzhao")
    plantDetail = UEditorField(verbose_name=u"植物详情", width=600, height=300, imagePath="website/ueditor/",filePath="website/ueditor/", default='')


    class Meta:
        verbose_name = u"植物信息"
        verbose_name_plural = verbose_name


class Animal(models.Model):
    GENDER_CHOICES = (
        ("invertebrates", u"海洋无脊椎动物"),
        ("protochordates", u"海洋原索动物"),
        ("vertebrate", u"海洋脊椎动物"),
    )
    animalName = models.CharField(max_length=50, verbose_name=u"动物标题")
    animalImage = models.ImageField(
        upload_to="image/%Y/%m",
        max_length=100,
        verbose_name=u"动物图片"
    )
    animalList = models.CharField(
        max_length=20,
        verbose_name=u"动物类别",
        choices=GENDER_CHOICES,
        default="protochordates")
    animalDetail = UEditorField(verbose_name=u"动物详情", width=600, height=300, imagePath="website/ueditor/",filePath="website/ueditor/", default='')


    class Meta:
        verbose_name = u"动物信息"
        verbose_name_plural = verbose_name


class Islands(models.Model):
    GENDER_CHOICES = (
        ("Continentalisland", u"大陆岛"),
        ("Oceanisland", u"海洋岛"),
        ("Alluvialisland", u"冲积岛"),
        ("Coralisland", u"珊瑚岛"),
    )
    isandsName = models.CharField(max_length=50, verbose_name=u"岛屿名称")
    isandsImage = models.ImageField(
        upload_to="image/%Y/%m",
        max_length=100,
        verbose_name=u"岛屿图片"
    )
    isandsList = models.CharField(
        max_length=20,
        verbose_name=u"岛屿类别",
        choices=GENDER_CHOICES,
        default="protochordates")
    isandsDetail = UEditorField(verbose_name=u"岛屿详情", width=600, height=300, imagePath="website/ueditor/",filePath="website/ueditor/", default='')


    class Meta:
        verbose_name = u"岛屿信息"
        verbose_name_plural = verbose_name


class MessageBoard(models.Model):
    GENDER_CHOICES = (
        ("qq", u"QQ号"),
        ("weixin", u"微信号"),
        ("phone", u"手机号"),
    )
    MessageName = models.CharField(max_length=50, verbose_name=u"留言标题")
    MessageList = models.CharField(
        max_length=20,
        verbose_name=u"联系方式",
        choices=GENDER_CHOICES,
        default="weixin")
    MessageDetail = UEditorField(verbose_name=u"留言内容", width=600, height=300, imagePath="website/ueditor/",filePath="website/ueditor/", default='')


    class Meta:
        verbose_name = u"留言信息"
        verbose_name_plural = verbose_name


class Encyclopedias(models.Model):
    GENDER_CHOICES = (
        ("book", u"图书"),
        ("game", u"游戏"),
        ("tanmi", u"探秘"),
        ("video", u"视频"),
    )
    baikeName = models.CharField(max_length=50, verbose_name=u"标题")
    baikeList = models.CharField(
        max_length=20,
        verbose_name=u"类别",
        choices=GENDER_CHOICES,
        default="book")
    baikeDetail = UEditorField(verbose_name=u"百科内容", width=600, height=300, imagePath="website/ueditor/",filePath="website/ueditor/", default='')
    baikevideo = models.FileField(upload_to='video', verbose_name=u"百科视频")


    class Meta:
        verbose_name = u"百科信息"
        verbose_name_plural = verbose_name

Traceback:

  File "C:\Users\1002149271\Desktop\开发兼职\django-海洋系统\DjangoMarineEducation\xadmin\views\base.py", line 336, in get_nav_menu
    site_menu = list(self.get_site_menu() or [])
  File "C:\Users\1002149271\Desktop\开发兼职\django-海洋系统\DjangoMarineEducation\website\adminx.py", line 20, in get_site_menu
    {'title': '用户信息', 'url': self.get_model_url(User, 'changelist')},
  File "C:\Users\1002149271\Desktop\开发兼职\django-海洋系统\DjangoMarineEducation\xadmin\views\base.py", line 148, in get_model_url
    args=args, kwargs=kwargs, current_app=self.admin_site.name)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\base.py", line 88, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 632, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'website_user_changelist' not found. 'website_user_changelist' is not a valid view function or pattern name.
[24/Feb/2019 17:43:03] "GET /admin/ HTTP/1.1" 500 201696

你期待的结果是什么?实际看到的错误信息又是什么?

NoReverseMatch at /admin/ Reverse for 'website_user_changelist' not found. 'website_user_changelist' is not a valid view function or pattern name.

以前不自定义时都是正常显示

Yuchener avatar Feb 24 '19 10:02 Yuchener

In your code I do not see the registration of the User model.

Cause of error

{'title': '用户信息', 'url': self.get_model_url(User, 'changelist')}

Do xadmin.site.register(User)

alexsilva avatar Feb 24 '19 12:02 alexsilva

In your code I do not see the registration of the User model.

Cause of error

{'title': '用户信息', 'url': self.get_model_url(User, 'changelist')}

Do xadmin.site.register(User)

Yes,I found out in the afternoon that the problem had been solved. Thank you very much.

Yuchener avatar Feb 24 '19 15:02 Yuchener