djangodjango-viewsdjango-urlsdjango-subdomains

how to make subdomains in django?


I want to make a panel which has two main urls by the name user and admin and also each of the "user" and "admin" urls have other urls. for example -> " site.com/panel/user/id/profile " OR " site.com/panel/admin/id/addItem " . but idk how can I make this main urls and subdomain urls :) ->

ecoverde/urls.py :

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('base.urls')),
    path('panel/', include('panel.urls')),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

ecoverde/panel/urls.py ((this is where i want to add two main urls and other sub urls) :

from .views import userPanel, Compare, saves
from django.urls import path



urlpatterns = [
    #main urls that i want
    path('user dashboard/<str:pk>', userPanel, name='userPanel'),
    path('admin dashboard/<str:pk>', adminPanel, name='adminPanel'),
    #sub urls that i want
    path('compare', Compare, name='compare'),
    path('saves', saves, name='saves'),
    #...
]

Solution

  • If you are using class-based views, inside of the view you add the function that you would like to accompany your sub-domain. For example, in userPanel view, if you want to have the subdomain /userPanel/validate/, by using djangos action decoration from rest_framework.decorators import action, the function could look like this @action(methods=['get'], detail=False, url_path="validate") def validate(self, request): your function

    url_path defaults to your function name if not specified.