django include
Import the
include
function:python from django.urls import include
Define a URL pattern using
include
:python urlpatterns = [ # other URL patterns path('app/', include('myapp.urls')), ]
Create a separate
urls.py
file in the app directory: ```python # myapp/urls.py from django.urls import path from . import views
urlpatterns = [ path('view/', views.my_view), ] ```
- Define a view function in the app's
views.py
file: ```python # myapp/views.py from django.http import HttpResponse
def my_view(request): return HttpResponse("This is my view.") ```
Ensure that the app is included in the Django project's settings:
python # settings.py INSTALLED_APPS = [ # other apps 'myapp', ]
Run the Django development server:
bash python manage.py runserver
Access the included view in the browser:
http://localhost:8000/app/view/