Django - 在Custom AdminSite

时间:2018-05-05 00:36:47

标签: django django-models django-admin

我有一个包含三个应用程序的django项目:网站,博客和customauth。 customauth app实现了自定义用户和AdminSite。博客应用程序有一个Post模型。我试图用自定义AdminSite注册Post模型,我收到以下错误:

NoReverseMatch at /my_admin/
Reverse for 'app_list' with keyword arguments '{'app_label': 'blog'}' not found. 1 pattern(s) tried: ['admin\\/(?P<app_label>customauth)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/my_admin/
Django Version: 2.0.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'app_list' with keyword arguments '{'app_label': 'blog'}' not found. 1 pattern(s) tried: ['admin\\/(?P<app_label>customauth)/$']

这是customauth / admin.py:

class MyAdminSite(admin.AdminSite):
    site_title = "Custom Administration Area"
    site_header = 'Custom Administration Area'

    def get_urls(self):
        urls = super(MyAdminSite, self).get_urls()
        my_urls = [
            url(r'^my_view/$', self.my_view, name='my_view'),
        ]
        return my_urls + urls

    def my_view(self,request):
        value = "You're looking at the new admin view."
        context = dict(
           # Include common variables for rendering the admin template.
           self.each_context(request),
           # Anything else you want in the context...
           key=value,
        )
        return TemplateResponse(request, "website/admin.html", context)

admin_site = MyAdminSite(name='myadmin')
# Now register the new UserAdmin...
admin_site.register(MyUser, UserAdmin)

这是博客/ admin.py:

from django.contrib import admin    
from customauth.admin import admin_site
# Register your models here.
from .models import Post

class PostAdmin(admin.ModelAdmin):
    pass

admin_site.register(Post,PostAdmin)

这是网站/ urls.py:

from django.contrib import admin
from django.urls import include, path
from customauth.admin import admin_site

from . import views

urlpatterns = [
    path('admin/',       admin.site.urls,            name='admin'),
    path('my_admin/',    include('customauth.urls'), name='custom_admin'),
    path('',             views.index,                name='index'),
    path('latest_news/', include('blog.urls'),       name='latest_news')
]

这是customauth / urls.py:

from . import views
from .admin import admin_site

urlpatterns = [
    path('', admin_site.my_view, name='custom_admin'),
]

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我弄清楚我做错了什么。

问题在于我配置网址的方式。

在website.urls中,我改变了:

import { Component } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';

@Component({
  selector: 'my-app',
  template: `
      <div id="myId">
          <div [innerHtml]="myStyle"></div>
          <h1>Hello</h1>
      </div>
  `
})
export class AppComponent {
  myStyle: SafeHtml;

  constructor(private _sanitizer: DomSanitizer) { }

  ngOnInit() {
    this.myStyle =
      this._sanitizer.bypassSecurityTrustHtml(`<style>#myId h1{color: red}</style>`);
  }
}

path('my_admin/',    include('customauth.urls'), name='custom_admin'),

这解决了这个问题。我不再需要customauth.urls文件。