Wednesday, September 19, 2018

Re: Creating urls

Your problem is caused by not properly structurizing your url files. An example (just for reference) is shown below:

/myappointments/urls.py
# includes...
urlpatterns
= [
    path
('admin/', admin.site.urls),
    path
('', include('clinic.urls'), namespace='clinic'),  # for example.com/<something> where "something" part is gonna be searched in /clinic/urls.py
    path
('', include('doctors.urls'), namespace='doctor'),  # for example.com/<something> where "something" part is gonna be searched in /doctors/urls.py
]

WARNING: Keep in mind that django resolves urls from top to bottom and first resolved path is gonna be opened so if you have a doctor with the same name as clinic, clinic would be resolved (you can change that by switching them putting doctors above clinics). You can do as many of them as you want (but it may lengthen loading time).

/clinic/urls.py
]# includes...
urlpatterns
= [
    path
('newclinic', views.NewClinic.as_view(), name='newclinic'),  # you can resolve it's url by 'clinic:newclinic', e.g. in templates
    path
('<str:cliniclabel>', views.ClinicDetail.as_view(), name='detail'),  # for example.com/clinic_label (keep in mind the problem of name uniqueness), resolved by 'clinic:detail' with label parameter
    path
('<str:cliniclabel>/<str:cliniclabel>', views.ClinicInClinic.as_view(), name='cc'),  # example.com/clinic_label/clinic_label (clinic:cc clinic1_label clinic2_label), keep in mind that combining those in one view may be a bit tricky...
    path
('<str:cliniclabel>/doctors', views.ClinicDoctorsList.as_view(), name='doctors'), # example.com/clinic_label/doctors ('clinic:doctors' with label),
]

/doctors/urls.py
# includes...
urlpatterns
= [
    path
('newdoctor', views.NewDoctor.as_view(), name='newdoctor'),  # example.com/newdoctor // 'doctor:newdoctor'
    path
('<str:doctorlabel>', views.DoctorDetail.as_view(), name='detail'),  # example.com/doctor_label //'doctor:detail' with label
]

For more, see docs.
BTW: My answer was not tested so I hope I haven't made many mistakes...

W dniu poniedziałek, 17 września 2018 12:43:20 UTC+2 użytkownik Joel napisał:
I have a project named myappointments, which has two apps, clinic and appointments. As of now, I am using the following urls in clinic.urls:

path('newclinic', views.newclinic, name="newclinic"),
path('<str:cliniclabel>/', views.clinic_home, name="clinic_home"),
path('<str:cliniclabel>/doctors', views.doctorlist, name="doctorlist"),
path('permissions', views.set_perms, name="set_perms"),

I want to call a view in clinic when a direct url is called.
Eg. When I visit mysite.com/newclinic and mysite.com/otherclinic/doctors, I need the clinic.view to be called at the appropriate function.
I tried the following in myappointments.urls:

urlpatterns = [
path('admin/', admin.site.urls),
path('appointments/', include('appointments.urls')),
path('clinic/', include('clinic.urls')),
path('', include('appointments.urls')),
path('newclinic', clinic.views.newclinic, name="newclinic"),
path('<str:cliniclabel>/', clinic.views.clinic_home, name="clinic_home"),
path('<str:cliniclabel>/doctors', clinic.views.doctorlist, name="doctorlist"),
path('permissions', clinic.views.set_perms, name="clinic_set_perms"),
]

But I get the error:
  File "/home/joel/myappointments/myappointments/urls.py", line 24, in <module>
    path('newclinic', clinic.views.newclinic, name="newclinic"),
NameError: name 'clinic' is not defined

I also tried within myappointments.urls:
urlpatterns = [
path('admin/', admin.site.urls),
path('appointments/', include('appointments.urls')),
path('clinic/', include('clinic.urls')),
path('', include('appointments.urls')),
path('newclinic', include('clinic.urls')),
path('<str:cliniclabel>/', include('clinic.urls')),
path('<str:cliniclabel>/doctors', include('clinic.urls')),
path('permissions', include('clinic.urls')),
]

But that caused some weird behavior.
What's the proper way to do this?
Sincerely yours,

Joel G Mathew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4bb1f3cf-3d81-457b-94e5-38b46e90710e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment