views.py
from django.views.generic import ListView
from .models import Staff
class StaffListView(ListView):
model = Staff
template_name = 'staff/staff_list.html'
def get_queryset(self):
return Staff.objects.filter(websites__path=self.kwargs['web'])
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('<str:web>/staff/', include('staff.urls')),
# I want to set web=chemical, if url is http://127.0.0.1:8000/staff
]
CodePudding user response:
With Django URL you cannot do that. But I have a cheat way. You can use middlewares
> Check the URL if this one is a URL validated with empty param > redirect it to the correct URL.
Sample:
def redirect_middleware(get_response):
def middleware(request, *args, **kwargs):
# In here I write a sample with a basic check, you can use regex to check.
if request.path == "/staff":
return redirect('/chemical/staff')
response = get_response(request, *args, **kwargs)
return response
return middleware
CodePudding user response:
A simple solution is to use RedirectView ?
urlpatterns = [
path('admin/', admin.site.urls),
path('<str:web>/staff/', include('staff.urls')),
path('staff', StaffRedirectView.as_view()),
]
# in views
from django.views.generic.base import RedirectView
class StaffRedirectView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
return "/chemical" self.request.get_full_path()
I think, this idea can be work for your case
CodePudding user response:
urlpatterns = [
path('admin/', admin.site.urls),
path('staff/', include('staff.urls'),kwargs={'web':''}),
path('<str:web>/staff/', include('staff.urls')),
]
staff.urls.py
urlpatterns = [
path('',views.StaffListView.as_view())
]