grappelli dashboard ValueError on two admin sites - django

I want use two admin sites for my project. Each with grappelli dashboard. I've executed this commands:
python manage.py customdashboard dashboard.py
python manage.py customdashboard dashboard.py
twice (once in project/project and second time in project/app)
#file system
project
project
dashboard.py
urls.py
app
dashboard.py
admin.py
#settings.py
GRAPPELLI_INDEX_DASHBOARD = {
'django.contrib.admin.site': 'project.dashboard.CustomIndexDashboard',
'app.admin.operator_site': 'app.dashboard.CustomIndexDashboard',
}
#urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
from app.admin import admin_site
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^myadmin/', include(operator_site.urls)),
url(r'^grappelli/', include('grappelli.urls')),
)
#app/admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
pass
admin_site = MyAdminSite()
The problem is when I go to /admin/ everything is ok, but when I go to /myadmin/ , I've got ValueError
Dashboard matching "{'app.admin.operator_site': 'app.dashboard.CustomIndexDashboard', 'django.contrib.admin.site': 'project.dashboard.CustomIndexDashboard'}" not found
full error trace: http://pastebin.com/w8W2eRPd
Where is the problem?

Ok, i've found it out. When making a subclass of a AdminSite on make an instance
admin_site = MyAdminSite()
you should use a custom name parameter (not 'admin'):
admin_site = MyAdminSite(name='myadmin')

Related

Routing does not work in Django: page not found (GET)

I've tried multiple methods of writing views but I don't think it is a problem here. App is installed in settings.py
It displays error every time.
project structure:
structure
views.py (app folder)
from django.http import HttpResponse
from django.shortcuts import render
def home_view(request):
return HttpResponse('Hello World')
url.py in apps folder
from django.urls import path
from . import views
urlpatterns = [
path('home_view/', views.home_view)
]
apps.py in app folder
from django.apps import AppConfig
class AppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app'
urls.py in store folder
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/home_view/', include('app.url')),
path('admin/', admin.site.urls),
]
error message:
error
As a django web development expert I saw some small corrections to be done:
In store urls.py file its app/ and app.urls
from django.contrib import admin
from django.urls import path, include
from app import views
urlpatterns = [
path('app/', include('app.urls')),
path('admin/', admin.site.urls),
]
Then change the app url.py file name to standard name urls.py.
also don't forget to add your app to installed_apps variable in settings.py file:
INSTALLED_APPS=[
'app',
'django.contin.auth',
#and other already specified apps
]
Rest all files have no bugs!!
According to this the correct path for HttpResponse is:
http://localhost:8000/app/home_view/
OR
http://127.0.0.1:8000/app/home_view/
In your urls.py file
Change
path('app/home_view/', include('app.url')),
To
path('', include('app.url')),
Then
On your browser go to:
127.0.0.1:8000/home_view/
The main thing it must be app.urls not app.url. change your file to url.py to urls.py, its recommended.
If you have defined path('app/home_view/', include('app.urls')) in urls.py of your store folder, then it goes to your urls.py which is in app.
In your app's urls.py you have written path('home_view/',views.home_view).
It means if you type 127.0.0.1:8000/app/home_view/home_view/ then it will render your HttpResponse that is Hello world.

Django Oscar change URL pattern

I have setup a django-oscar project and I'm trying to configure the URLs. My goal is to change /catalogue to /catalog.
As per the documentation I've added app.py in myproject/app.py
myproject/app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urlpatterns = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
return urlpatterns
application = MyShop()
myproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from .app import application
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
url(r'^index/$',views.index, name = 'index'),
]
The project server runs without any error, but when I try localhost:8000/catalog I get
NoReverseMatch at /catalog/ 'customer' is not a registered namespace.
The expected output is localhost:8000/catalog should return the catalogue page.
You can try this
in app.py
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
# Override get_urls method
def get_urls(self):
urls = [
url(r'^catalog/', include(self.catalogue_app.urls)),
# all the remaining URLs, removed for simplicity
# ...
]
urls = urls + super(MyShop,self).get_urls()
return urls
application = MyShop()
And in your urls.py
you can simply add this
from myproject.app import application as shop
url(r'', shop.urls),
Hope it help for you
Expanding on c.grey's answer to specify how to replace instead of add the urls -
from django.conf.urls import url, include
from oscar import app
class MyShop(app.Shop):
def get_urls(self):
urls = super(MyShop, self).get_urls()
for index, u in enumerate(urls):
if u.regex.pattern == r'^catalogue/':
urls[index] = url(r'^catalog/', include(self.catalogue_app.urls))
break
return urls
application = MyShop()
You need to include the URLs, not reference them directly.
url(r'', include('application.urls')),

django urls: "Django tried these URL patterns"

I am trying a tutorial on Django called blog. I have the following structure:
FirstBlog|FirstBlog
settings
urls
__init__
etc
blog
templates | index.html
migrations
views.py
manage.py
The view.py has
from django.shortcuts import render
from django.shortcuts import render_to_response
from blog.models import posts
def home(request):
return render('index.html')
The urls.py has
from django.conf.urls import url
from django.conf.urls import include
from django.contrib import admin
urlpatterns = [
url(r'^blog', 'FirstBlog.blog.views.home',name='home'),
]
and I get this error:
Using the URLconf defined in FirstBlog.urls, Django tried these URL patterns, in this order: ^blog [name='home']
The current URL, , didn't match any of these.
I can't seem to get it right..
Any help would be appreciated.
Thank you,
You are requesting for / url and you have not saved any such mapping. Current mapping is for /blog . So it will work for the same url.
i.e goto the browser and request /blog
If you need it to work for / then change the urls appropriately.
within your blog app, create a urls.py file and add the following code which calls the home view.
blog/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
#url(r'^',views.home, name='home'),
]
then in your root urls file which can be found at FirstBlog/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^blog/',include('blog.urls')), #when you visit this url, it will go into the blog application and visit the internal urls in the app
]
PS:
your templates should be in blog/templates/blog/index.html
Read this docs on templates to understand how django locates templates.
This one is to understand how urls work Urls dispatcher
You are doing this in the wrong way! Try doing that using TemplateView from class-based views, which are the current standard from django views.
Check the django documentation: https://docs.djangoproject.com/en/1.9/topics/class-based-views/
Use this at the urls.py:
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
url(r'^blog/', TemplateView.as_view(template_name="index.html")),
]
And then, create a folder called templates in the project root ( like this: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#write-views-that-actually-do-something ) named index.html
Simply go to file then select Save all your project instead of save. Or use shortcut Ctrl +k s on windows. Project should be able to sync and display the code on Django interface

django ImportError at /admin/ no module named todo

I am following a tutorial on http://www.lightbird.net/dbe/todo_list.html to create a simple todo app. In one of the steps, I had to modify view to add an ability in 'admin' to mark tasks as done from that view. However I get the error ImportError at /admin/ no module named todo.
The error is not thrown from any particular line from the code so I do not know how to debug this. I am new to django. So I documented my error in my blog here: http://djangounchain.wordpress.com/2013/01/10/tutorial-8-todo-list-app/
Hope someone can help me!
You are registering your models to AdminSite in todo/models.py itself.
As per official django documentation, you need to create admin.py file inside your app for admin.autodiscover() to work properly.
The last step in setting up the Django admin is to hook your AdminSite
instance into your URLconf. Do this by pointing a given URL at the
AdminSite.urls method.
In this example, we register the default AdminSite instance
django.contrib.admin.site at the URL /admin/
# urls.py
from django.conf.urls import patterns, url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
Above we used admin.autodiscover() to automatically load the
INSTALLED_APPS admin.py modules.

Issues with 2 different admin sites in a Django

I want to have 2 separate admin sites inside a Django project.
First is default admin
And Second is "coursemanager"
Following code i have added with the help of AdminSite
File Path "cms/courses/admin.py"
from courses.models import *
from django.contrib import admin
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User, Group
from django.contrib.auth.admin import UserAdmin, GroupAdmin
class CourseManager(AdminSite):
name = 'CourseManager'
course_manager = CourseManager()
class CityAdmin(admin.ModelAdmin):
list_display = ['__unicode__', 'status',]
list_filter = ['status',]
search_fields = ['title',]
course_manager.register(City, CityAdmin)
"cms/cms/urls.py"
from courses.admin import course_manager
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'coursemanager/', include(course_manager.urls)),
)
But when i add this http://domain.local/coursemanager/ & http://domain.local/admin/ both panel is working but in http://domain.local/coursemanger/ list register city module but not showing the add or changes links. Event i tried to access links from url but not working. I am checking this as superuser and i have all modules access. But when i change code like bellow
"cms/cms/urls.py"
from courses.admin import course_manager
urlpatterns = patterns('',
url(r'coursemanager/', include(course_manager.urls)),
url(r'^admin/', include(admin.site.urls)),
)
Then http://domain.local/coursemanager/ working properly and http://domain.local/admin panel is only listing all register admin but not showing the add/change links.
The app_name of AdminSite() is initialized through AdminSite.__init__(). You cannot override it by providing class-level variable, thus you were experiencing instance namespace collision and then some reverse failure, here, which caused add/change links not showing. Try
class CourseManager(AdminSite):
'...'
course_manager = CourseManager(name='CourseManager')
# or
course_manager = AdminSite(name='CourseManager')