Issues with 2 different admin sites in a Django - 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')

Related

Django: how to route URL requests to django.contrib.admin functions

I would like Django to return the same response for requests to /myapp/add as /admin/myapp/mymodel/add.
myproject/myapp/models.py defines the model and myapp/admin.py registers with django.contrib.admin.
myproject/myapp/models.py:
from django.db import models
class MyModel(models.Model):
...
myproject/myapp/admin.py:
from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)
I am stuck on how to route the request to django.contrib.admin in the project's urlpatterns:
myproject/myproject/urls.py:
urlpatterns = [
url(r'^$', views.home_page, name='home'),
url(r'^admin/', admin.site.urls),
url(r'^myapp/add', ??????),
]
From printing the return from resolve('/admin/myapp/mymodel/add/') this looks like part of the answer:
ResolverMatch(func=django.contrib.admin.options.add_view, args=(), kwargs={}, url_name=myapp_mymodel_add, app_names=['admin'], namespaces=['admin'])
Well let me say that seems like a weird thing to do, but anyway:
in the file: django.contrib.admin.options.py we see:
class ModelAdmin(BaseModelAdmin):
we see that the add view returns
_changeform_view()
which uses the template
django/contrib/admin/templates/change_form.html
So you would want to render that template in your view.
but it would be missing a bunch of context items,
so you would basically have to re-implement the django.admin.options._changeform_view
and then copy the template django/contrib/admin/templates/change_form.html to your apps' template directory
def admin_add(request):
# admin.changeform_view code here
return render(request, "myapp/change_form.html", {context{)
ps. I assume the admin site view assumes the user is the "superuser" and not a normal user so you would want to account for that as well..

Mezzanine ignores the view, displays the template but does not pass the context

I created a view for my model, with the corresponding urls and template files. Then, in the admin panel, I have created a Rich text page, specifying the same URL (ingredients) defined in urlpatterns. Mezzanine ignores the view, displays the template but does not pass the context.
How can I solve it?
These are the codes:
models.py
from django.db import models
from mezzanine.pages.models import Page
from django.utils.translation import ugettext_lazy as _
class Ingredient(Page):
name = models.CharField(max_length=60)
information = models.TextField(null=True, blank=True, verbose_name=_("Description"))
views.py
from django.template.response import TemplateResponse
from .models import Ingredient
def ingredients(request):
ingredients = Ingredient.objects.all().order_by('name')
templates = ["pages/ingredients.html"]
return TemplateResponse(request, templates, {'ingredients':ingredients})
urls.py
from django.conf.urls import url
from .views import ingredients
urlpatterns = [
url("^$", ingredients, name="ingredients"),
]
TemplateResponse does not expect the request in its arguments. See the docs.
return TemplateResponse(templates, {'ingredients':ingredients})
However I expect you meant to use the standard render function there:
return render(request, "pages/ingredients.html", {'ingredients':ingredients})
Ok, the solution has been define my app urls before any other definition in my project urls.py file.
project_name/project_name/urls.py
# Add the urlpatterns for any custom Django applications here.
# You can also change the ``home`` view to add your own functionality
# to the project's homepage.
urlpatterns = [
url(r'^ingredients/', include("apps.ingredients.urls")),
]
urlpatterns += i18n_patterns(
# Change the admin prefix here to use an alternate URL for the
# admin interface, which would be marginally more secure.
url("^admin/", include(admin.site.urls)),
)

Django: Issue with url conf

I'm trying to change my project to use class based views, and I'm running into a strange error with my url conf
I have a classed based view:
class GroupOrTeamCreate(AjaxableResponseMixin, CreateView):
model = GroupOrTeam
fields = ['name', 'description']
#success_url = reverse('home_page') # redirect to self
I have the last line commented out because if I don't, django complains that there are no patterns in my url conf.
To start, here's my base urls.py
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='core/home.html'), name='home_page'),
url(r'^administration/', include('administration.urls', app_name='administration')),
url(r'^reports/', include('reports.urls', app_name='reports')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Clearly there are patterns in there. If I comment out the administration urls, it works. So I assume the problem is in there somewhere.
administration urls.py
from django.conf.urls import patterns, url
from .views import ActiveTabTemplateView, GroupOrTeamCreate, GroupOrTeamUpdate
urlpatterns = patterns('',
# Add page
url(r'^add/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'), name='add_page'),
url(r'^add/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'),
name='add_page'),
# Seach page
url(r'^search/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'), name='search_page'),
url(r'^search/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'),
name='search_page'),
#--------------------------------------------------------------------------
# Forms
#--------------------------------------------------------------------------
# Groups and teams
url(r'^group-or-team-form/$', GroupOrTeamCreate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
url(r'^group-or-team-form/(?P<pk>\d+)/$',
GroupOrTeamUpdate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
)
I'm not seeing the problem.. these pages load just fine without that reverse statement in, it appears to be the introduction of the reverse statement that breaks everything but I can't for the life of me work out what the cause is.
You get the error because the URL conf has not been loaded yet when the class is defined.
Use reverse_lazy instead of reverse.
from django.core.urlresolvers import reverse_lazy
success_url = reverse_lazy('home_page')

grappelli dashboard ValueError on two admin sites

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')

Models not showing in Admin Interface

I can not get my models to display within the Admin Interface even after registering them in django-admin.py using admin.site.register(topic).
I have registered a model class topic, but it just isn't showing up in the interface. Instead, I'm getting groups and users in the auth section and sites in the sites section.
Below is the code I currently have. Any help would be appreciated.
models.py
class topic(models.Model):
topic_name = models.CharField(max_length=30)
description=models.CharField(max_length=255,null=True, blank = True)
class Admin:
pass
def str__(self):
return '%s''--' %(self.topic_name)
admin.py
from django.contrib import admin
from edc.kds.models import *
if __name == "main":
management.execute_from_command_line()
admin.site.register(topic)
You're doing some strange stuff.
1: You have some ancient, years old class Admin syntax which isn't necessary.
2: You have a strange if __name block in there. Clearly that should raise a NameError, but assuming you actually wrote __name__ == 'main', there's your problem.
__name__ is set to 'main' only if the file is directly executed. If it's executed by django machinery, the if block will never fire, and thus admin.site.register will never be called.
Where'd you get this idea?
# 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)),
)