Search code examples
pythondjangourl-pattern

How do I set up my Django urlpatterns within my app (not project)


Let's say I've got the classic "School" app within my Django project. My school/models.py contains models for both student and course. All my project files live within a directory I named config.

How do I write an include statement(s) within config/urls.py that references two separate endpoints within school/urls.py? And then what do I put in schools/urls.py?

For example, if I were trying to define an endpoint just for students, in config/urls.py I would do something like this:

from django.urls import path, include

urlpatterns = [
    ...
    path("students/", include("school.urls"), name="students"),
    ...
]

And then in school/urls.py I would do something like this:

from django.urls import path
from peakbagger.views import StudentCreateView, StudentDetailView, StudentListView, StudentUpdateView, StudentDeleteView

urlpatterns = [
    # ...
    path("", StudentListView.as_view(), name="student-list"),
    path("add/", StudentCreateView.as_view(), name="student-add"),
    path("<int:pk>/", StudentDetailView.as_view(), name="student-detail"),
    path("<int:pk>/update/", StudentUpdateView.as_view(), name="student-update"),
    path("<int:pk>/delete/", StudentDeleteView.as_view(), name="student-delete"),
]

But how do I do I add another urlpattern to config/urls.py along the lines of something like this? The include statement needs some additional info/parameters, no?

from django.urls import path, include

urlpatterns = [
    ...
    path("students/", include("school.urls"), name="students"),
    path("courses/", include("school.urls"), name="courses"),
    ...
]

And then what happens inside of school/urls.py?

I'm open to suggestions, and definitely am a neophyte when it comes to the Django philosophy. Do I need an additional urls.py somewhere? I'd prefer not to put everything in config/urls.py and I'd prefer not to build a separate app for both students and courses.


Solution

  • I would rather create two (or more) urls.py files and then point them separately.

    # directory structure
    school/
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    ├── urls
    │   ├── course.py
    │   ├── __init__.py
    │   └── student.py
    └── views.py
    
    
    # school/urls/course.py
    from django.urls import path
    from school.views import CourseListView
    
    urlpatterns = [
        path("", CourseListView.as_view(), name="course_list"),
        # other URLs
    ]
    
    
    
    # school/urls/student.py
    from django.urls import path
    from school.views import StudentListView
    
    urlpatterns = [
        path("", StudentListView.as_view(), name="student_list"),
        # other URLs
    ]
    
    
    # config/urls.py
    from django.urls import include, path
    
    urlpatterns = [
        path("student/", include("school.urls.student")),
        path("course/", include("school.urls.course")),
        # other URLs
    ]