I have two admin sites and I want to have different custom templates on both of them
This is my admin.py file:
from django.contrib import admin
from django.contrib.admin import AdminSite
from .models import Equipo
#admin.register(Equipo)
class EquipoAdmin(admin.ModelAdmin):
list_display = ('codigo', 'nombre', 'contador', 'unidades')
class AdminMantenimiento(AdminSite):
site_header = "MANTENIMIENTO"
class EquipoAdminMantenimiento(admin.ModelAdmin):
list_display = ('codigo', 'nombre')
admin_site = AdminMantenimiento(name='Administrador Mantenimiento')
admin_site.register(Equipo, EquipoAdminMantenimiento)
This is my urls.py file:
from django.contrib import admin
from django.urls import path
from Mantenimiento.admin import admin_site
urlpatterns = [
path('admin/', admin.site.urls),
path('admin2/',admin_site.urls)
]
If I override the templates as per Django documentation changes would be applied to both AdminSites. How can I set custom templates for the class extending AdminSite?
The only way I could think to do this would be to explicitly override the url of the admin page you want to customize and point it to a different template. For example to customize a template in admin2/ but not admin1/
from django.contrib import admin
from django.urls import path
from Mantenimiento.admin import admin_site
from app.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('admin2/app/model', TemplateView.as_view(template_name='model_admin.html')),
path('admin2/', admin_site.urls)
]
This will only work if the more specific url definition (admin2/app/model) comes before the less specific definition (admin2/)
Related
I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django, it is giving me this error message:
django.core.exceptions.ImproperlyConfigured: The included URLconf 'starsocial.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
The issue started when I added a new app which has a urls.py like the following:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r'login/$',
auth_views.LoginView.as_view(template_name='accounts/login.html'),
name='login'),
url(r'logout/$',auth_views.LogoutView.as_view(), name='logout'),
url(r'signup/$',views.SignUp.as_view(), name='signup'),
]
My project urls.py has a line which points to the app and looks like the following code:
from django.contrib import admin
from django.urls import path,include
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^admin/',admin.site.urls),
url(r'^$', views.HomePage.as_view(), name='home'),
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^test/$', views.TestPage.as_view(), name='test'),
url(r'^thanks/$', views.ThanksPage.as_view(), name='thanks')
]
My application's view looks like the following:
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse('login')
template_name = 'accounts/signup.html'
My project's view looks like:
from django.views.generic import TemplateView
class TestPage(TemplateView):
template_name = 'test.html'
class ThanksPage(TemplateView):
template_name = 'thanks.html'
class HomePage(TemplateView):
template_name = 'index.html'
Can anyone please help me identify where I could possibly be going wrong.
You are importing auth.urls twice. Remove url(r'^accounts/', include('django.contrib.auth.urls')) from your project's urls.py
I am importing wrong URL configuration, instead of 'reverse' I should import 'reverse_lazy',
change
from django.shortcuts import render
from django.urls import reverse
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse('login')
template_name = 'accounts/signup.html'
to
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from . import forms
# Create your views here.
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy('login')
template_name = 'accounts/signup.html'
I am using Django Built-in Admin panel, is there any way to remove app name from urls?
If I want to access User listing, it redirects me to 27.0.0.1:8000/admin/auth/user/ can I make it 27.0.0.1:8000/admin/user/ without the app name auth?
Thanks,
As documented here you can create a custom AdminSite and override the get_urls method. This simple code should to the job:
In your common.admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
def get_urls(self):
urlpatterns = super().get_urls()
for model, model_admin in self._registry.items():
urlpatterns += [
path('%s/' % (model._meta.model_name), include(model_admin.urls)),
]
return urlpatterns
my_admin = MyAdminSite('My Admin')
my_admin.register(YourModel)
...
Note that you register your models with the new custom AdminSite instance.
Then in your projects urls.py
from common.admin import my_admin as admin
from django.urls import path
urlpatterns = [
path('admin/', admin.urls),
# Your other patterns
]
I'm trying to create an api that uploads an image with an email to the database. But I'm getting an error "raise ImproperlyConfigured(msg.format(name=self.urlconf_name))" Is the problem in my urls.py?
https://imgur.com/OjPUhOv.jpg
This is how my structure looks
https://imgur.com/TW6pKPn.jpg
This is the error
for urls.py-
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('user.urls')),
path('api/',include('api_test.urls'))
# path('articles/',include('articles.urls'))
]
for api_test/urls.py
from django.urls import path,include
from django.conf import settings
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('image_test',views.api_test,base_name='image_test')
urlpatterns = [
# path('/',views.api_test),
path('',include(routers.url)),
]
for views.py
class api_test(viewsets.ModelViewSet):
queryset = fineDB.objects.all()
serializer_class = fineSerializer
##for serializers.py
from rest_framework import serializers
from .models import fineDB
class fineSerializer(serializers.ModelSerializer):
image = serializers.ImageField(max_length=None,use_url=True)
class Meta:
model = fineDB
fields = {'email','image'}
You should probably get the urls from router, not routers.
At the same time you don't need both the router and the urlpatterns in that file. You can import the router and mount it's router.urls in urls.py.
from rest_framework import routers
router = routers.DefaultRouter()
router.register('image_test',views.api_test,base_name='image_test')
urlpatterns = [
# path('/',views.api_test),
path('',include(router.urls)), # <-
]
I'm creating a multi user blog in Djano and would like to use the Admin view for all users to provide functionality to create, edit, and delete posts.
I found this tutorial which explains the use of Admin for non-staff users. However, I am getting an error
cannot import name user_admin_site
admin.py
from django.contrib import admin
from django.contrib.admin.sites import AdminSite
class UserAdmin(AdminSite):
pass
user_admin_site = UserAdmin(name='user')
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from myapp.admin import user_admin_site
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(user_admin_site.urls)),
url(r'^', include('myapp.urls')),
)
I haven't found a satisfactory way of doing this: I have a djangocms setup that is working fine. But I need to add content from a table outside the CMS to my homepage and render that content on the template. I can do this, but editing the urls.py within CMS to use my views like so...
url(r'^', 'myapp.views.slideshow_info'),
... excludes any content from CMS. I understand that I just get my custom views to accommodate what CMS' views is doing, but how do I achieve this?
at the moment my app's views says:
from myapp.models import model1, model2
def slideshow_info(request):
return render_to_response('index.html', {'slideshow_list' : model1.objects.all()})
Many thanks
You can hook a custom app instance to any Django-CMS page. Here's the documentation on how to do so: http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks You shouldn't need to alter the base url patterns to specifically re-route / to your view.
Before custom app-hooks were available, I would accomplish what you're trying to do with template tags.
Hope that helps you out.
Followup
Ok, in a recently completed site, I had to hook an app titled "portfolio" to display images on the home page of a Django-CMS site.
Here are the relevant portions of the code:
#portfolio/cms_app.py
from django.utils.translation import ugettext_lazy as _
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class PortfolioAppHook(CMSApp):
name = _('Portfolio')
urls = ['portfolio.urls']
apphook_pool.register(PortfolioAppHook)
#portfolio/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('portfolio.views',
url(r'^(?P<slug>[-\w]+)/$', 'project_detail', name='project_detail'),
url(r'^$', 'portfolio_index', name='portfolio_index'),
)
#portfolio/views.py
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render
from portfolio.models import Project
def portfolio_index(request):
project_objects = Project.for_public if request.user.is_anonymous() \
else Project.objects
projects = project_objects.all().select_related(depth=1)
return render('portfolio/index.html',
{'projects' : projects}, request)
def project_detail(request, slug):
project = get_object_or_404(Project, slug=slug)
if not project.public and request.user.is_anonymous():
return HttpResponseRedirect('/?login=true')
return render('portfolio/project_detail.html',
{'project' : project}, request)
#urls.py (base urls)
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from views import login_user, logout_user
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/filebrowser/', include('filebrowser.urls')),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^login/$', login_user, name='login_user'),
url(r'^logout/$', logout_user, name='logout_user'),
(r'^', include('sorl.thumbnail.urls')),
(r'^', include('cms.urls')),
)
if settings.SERVE_STATIC_MEDIA:
urlpatterns += patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
As you can see from this working example, I haven't altered my base URLs to accommodate the home page view, rather I've provided the URLs for my Portfolio app to Django-CMS through cms_app.py
Hope that gets you going.