Django secondary admin site links to main admin site - django

I'm trying to add a secondary admin site and it's working correctly except that any links are pointing to the main admin site. Not sure how to get around this.
If I visit /dispatch/admin/ the model Run is visible but when I click the link it directs me to /admin/dispatch/run/ instead of /dispatch/admin/dispatch/run/.
Django==3.2.7
class DispatchAdminSite(admin.AdminSite):
pass
class DispatchRunAdmin(RunAdmin):
def get_queryset(self, request):
return (
super().get_queryset(request)
.filter_today_or_future()
)
def get_readonly_fields(self, request, obj=None):
return [f.name for f in self.model._meta.fields]
dispatch_admin_site = DispatchAdminSite(name='Dispatch')
dispatch_admin_site.register(models.Run, DispatchRunAdmin)
dispatch/urls.py
app_name = 'dispatch'
urlpatterns = [
path('admin/', admin.dispatch_admin_site.urls),
]
project/urls.py
urlpatterns = [
path('dispatch/', include('dispatch.urls')),
path('admin/', admin.site.urls),
]

For whatever reason the custom admin can't be added using include it has to be a top level URL in your project.
# project/urls.py
from django.contrib import admin
from dispatch.admin import dispatch_admin_site
urlpatterns = [
path('dispatch-admin/', dispatch_admin_site.urls),
path('admin/', admin.site.urls),
]

Related

url is showing in page not found error in django?

I created an endpoint and registered it in urls.py. When I try to hit the endpoint I get a 404 error but on the 404 page the url is shown as one of the patterns django tried to match. Stumped.
api.py
class UpdateLast(viewsets.GenericViewSet, UpdateModelMixin):
def get(self):
return XYZModel.objects.all()
def partial_update(self, request, *args, **kwargs):
if request.user.is_superuser:
with transaction.atomic():
for key, value in request.data:
if key == 'tic':
XYZModel.objects.filter(ticker=request.data['tic']).filter(trail=True).update(
last_high=Case(
When(
LessThan(F('last_high'),
request.data['high']),
then=Value(request.data['high'])),
default=F('last_high')
)
)
urls.py (this is inside the app)
router = routers.DefaultRouter()
router.register('api/dsetting', DViewSet, 'dsetting')
router.register('api/update-last-high', UpdateLast, 'update-last-high')
urlpatterns = [
path('', include(router.urls))
]
urls.py (main)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_app.urls')),
When I hit the end-point api/update-last-high I get a 404 page.
On that page the exact end-point is shown as a pattern that django tried to match.
^api/update-last-high/(?P[^/.]+)/$ [name='update-last-high-detail']
^api/update-last-high/(?P[^/.]+).(?P[a-z0-9]+)/?$ [name='update-last-high-detail']
from rest_framework import routers
from . import views
from django.urls import path, include
router = routers.DefaultRouter()
router.register('api/dsetting', DViewSet, 'dsetting')
router.register('api/update-last-high', UpdateLast, 'update-last-high')
# you forgot to include the router.urls
urlpatterns = [
path('', include(router.urls))
]

How to add custom views or overide views for Django Admin index?

urls.py ->
''''from django.contrib import admin
from django.urls import path
from Administrator import views
admin.site.index_template = 'admin/dashboard.html'
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
]
'''
I have added a custom admin_index template in urls.py. My doubt is shown in the picture.
My Admin index custom template.Kindly:
You can override the AdminSite and then in this custom admin site class you can override the index method to provide additional context that you can then use in your template
class CustomAdminSite(admin.AdminSite):
def index(self, request, extra_context=None):
extra_context = extra_context or {}
extra_context['foo'] = 'bar'
return super().index(request, extra_context=extra_context)
best way to do this, the code below should work.
urlpatterns = [
path('admin/', admin.site.urls , {'extra_context': {
'pro': Product.objects.all(),}}),
]
Kindly Give your suggestions.
I have searching it for one month.

How to include other app's urls.py in get_absolute_url: reverse() django?

In app models.py, I am using get_absolute_url to reverse to a particular path using "name" that is in different app urls.py.
But of course I am getting error because the I have not created urls.py for that app as the urlpattern is already present in some other urls.py.
So is there any include(app.urls) type functionality that I can use in reverse?
#app: A - urls.py
urlpatterns = [
path('post/<int:pk>/', PostDetailView.as_view(), name = 'post-detail'),
...
]
#app: B - models.py
def get_absolute_url(self):
return reverse('post-detail',kwargs = {'post_id.id':self.post_id.id})
in your main urls.py file you have to add all your app urls as follows and call it in your get absolute method. You are defining this method to that particular model only, and is linked to the instance by self.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('your_app.urls')),
]
also you can modify your get absolute method as
def get_absolute_url(self):
return reverse('post-detail',kwargs = {"pk": self.pk)

URl redirection issue in Django 2

I'm new to Django and trying to build a web app with the following structure. I need your help to understand what Im doing wrong.
The flow of the application is shadesProductUploader.urls will forward '' to authSection for login and after a successful login user should be redirected to mainSection 'home/'.
Urls.py files are
shadesProductUploader.urls
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('authSection.urls')),
]
authSection.urls
from django.contrib import admin
from django.urls import path, include
from . import views
app_name = 'authSection'
urlpatterns = [
path('', views.login_view, name='login'),
]
mainSection.urls
from django.contrib import admin
from django.urls import path,include
from . import views
app_name = 'mainSection'
urlpatterns = [
path('home/', views.home),
]
and the view.py in authSection
def login_view(request):
next = request.GET.get('next')
form = userLoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
login(request,user)
if next:
return redirect(next)
context={'user':user}
return redirect('home/')
return render(request, 'login.html', {'form': form})
after a successful login I get this error.
What Am I missing? Not sure why I see a Url of home/ home/
Update shadesProductUploader's main section url like this:
urlpatterns = [
path('',include('mainSection.urls')),
... # other urls
]
And change in mainSection urls like this:
urlpatterns = [
path('home/', views.home, name="home"), # <-- added name here
]
And in view, use it like this:
if next:
return redirect(next)
context={'user':user}
return redirect(reverse('home'))
Here, we have named our url as home. And we got the url using reverse.

Make arbitrary url as homepage

I have such a urls.py configuration in project repository "forum"
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
]
and with article/urls.py as
#Article app's url config
urlpatterns = [
# show the article list
url(r"^list/(?P<block_id>\d+)$", views.article_list, name="article_list"),
I attempt to take "^article/list/1$" as home page instead of "views.index".
How to make it redirect to "^article/list/1$" when I issue request "127.0.0.1:8000"?
You can redirect from your views.index. In your views.py
from django.shortcuts import redirect
def index(request):
return redirect('article:article_list')
which should take you to article/list. You can include that block_id parameter in the redirect function.
try this
redirect(reverse('article:article_list', kwargs={'block_id':2}))
and make sure to add kwargs in function like this
article_list(request,**kwargs):
I assume you want to this to debug your article page.
You have two ways of doing this.
Either redirect your index view to article view
views.py
from django.shortcuts import redirect
def index(request):
#We force a redirect to article_list view, and we pass arguments.
#Notice it has to come first.
redirect ("article_list", article_list_argument_view=1)
#it will redirect to localhost:8000/article/list/1
'''
Your Home page code.
'''
Put the url directly before the include
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
#The order is important.
url(r'^article/list/1$', "article_list", {'article_list_argument_view':1})
url(r'^article/', include('article.urls',namespace='article')),
]