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

Time:11-23

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.

CodePudding user response:

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
]

CodePudding user response:

The best solution for you is to make separate urls directory inside your app For example if you have school as app then app

├── School
│   ├── views.py
│   └── models.py
|   └── urls
|       └── __init__.py
|       └── urls.py
|       └── school_urls.py
|       └── course_urls.py

Now in your main project urls you can set this way

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

and in urls.py of your school urls folder you can do this way

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

and you can do add course view in course url folder and another student view in student urls file

  • Related