Django: extend Admin for all users - python

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

Related

Prevent admin url from being overridden Django

I have a urls file that looks like this, however I cannot access the admin page. Is there any way to specify that the admin URL should never be overridden?
from django.contrib import admin
from django.urls import path
from latency import views
urlpatterns = [
path("admin/", admin.site.urls),
path("<str:chain>", views.regions),
path("<str:chain>/<str:region>", views.chain),
path("", views.index),
]```

Password reset function in django admin is not working

urls.py
from django.contrib import admin
from django.urls import path,include
from django.urls import re_path
from App.views import *
from.router import router
from django.views.generic import TemplateView
from rest_framework_simplejwt.views import ( TokenObtainPairView,TokenRefreshView)
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', TemplateView.as_view(template_name="social_app/index.html")), #social_app/index.html
path('admin/', admin.site.urls), #admin api
path('api/',include(router.urls)), #api
path('accounts/', include('allauth.urls')), #allauth
re_path('rest-auth/', include('rest_auth.urls')), #rest_auth
path('api-auth/', include('rest_framework.urls')),
re_path('/registration/', include('rest_auth.registration.urls')),
path('api/token/', MyObtainTokenPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('jobview/',job),
path('timelog/',timelogview),
path('chaining/', include('smart_selects.urls')),
path('admin/password_reset/',auth_views.PasswordResetView.as_view(),name='admin_password_reset',),
path('admin/password_reset/done/',auth_views.PasswordResetDoneView.as_view(),name='password_reset_done',),
path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(),name='password_reset_confirm',),
path('reset/done/',auth_views.PasswordResetCompleteView.as_view(),name='password_reset_complete',),
] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
I have given the password reset function in the admin page login, it is showing as "Forgotten your password or username?" in the page but when I click it the url changes http://127.0.0.1:8000/admin/login/?next=/admin/password_reset/ to this but the same login page is loading i didnt redirected to any other reset page.
I have tried but couldn't able to fix it, kindly help me to fix this issue.
Add all urls with admin before path('admin/', admin.site.urls)
Then add this line of code in your admin.py file and run the server and connect with your admin account then go to Site model and edit domain example.com with http://127.0.0.1:8000 when you're in development (in production it will be your server link) and edit name example.com with whatever name you want for your site.
from django.contrib.sites.models import Site
from django.contrib import admin
#admin.register(Site)
class SiteAdmin(admin.ModelAdmin):
list_display = ('domain', 'name')
search_fields = ('domain', 'name')

Custom templates for custom AdminSite Django

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

Django url Routing Error

I have a very basic url router in my django project:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = staticfiles_urlpatterns()
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^/?', include('customApp.urls')),
)
When I start the dev server and go to 127.0.0.1:8000/admin/, I get a ViewDoesNotExist at /admin/ error.
Here is the content of the exception:
Could not import customApp.views.event. View does not exist in module customApp.views.
I've already tried re-ordering the urls (I have no idea how that would help, but I tried it anyway) and changing r'^/?' to r'^/'.
When I comment out the last url, the admin page works again.
Here's the customApp.urls code:
from django.conf.urls import patterns, include, url
import django.contrib.auth.views
import django.contrib.auth
urlpatterns = patterns('customApp.views',
url(r'^$', 'index'),
url(r'^rest/v1/event/add/$', 'event'),
url(r'^rest/v1/reports/$', 'reports'),
)
urlpatterns += patterns('',
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
It is simple buddy. Django cannot find customApp views. Please ensure whatever views you have got in urls.py, it should exist.
Thanks

Django Generic Views Date-Based URLconf

Trying to teach myself Django but running into a snag.
Generic Views seem to be a great idea but I personally find the documentation a little cryptic at times (maybe I'm being prissy).
So I have been trying to use the Date Based generics views in and specifically ArchieveIndexView.
I have even attempted following some non-djangoproject.com examples and still have problems.
I used the example provided at this site.
Here is my current project/urls.py.
I am also at this point, not worrying about pattern matching, just trying to get it to work.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
With this setup I keep receiving a Invalid Syntax error at the line describing ArchiveIndexView class.
If I comment out this line the problem goes away. If I decouple the URLs to their appropriate app I get the same error.
The error suggest I just have something out of place, a comma or something but I have yet to conclude what it is.
Thank you!
use the below code
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view({'date_field': 'pub_date', 'queryset': Entry.objects.all()})),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
You seem to forget the {} brace required for dict in url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
line.
Ah. I solved my own question thanks to a little pushing from shiva.
The dictionary works but only for the extra_content argument.
It was just done like that on the website I was trying to copy and for extra content in the documentation so I kept overlooking that glaringly obvious problem.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view(date_field='pub_date', queryset=Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Just needed to sleep on it...

Categories