-1

I am trying to redirect an link from app to another page view function. But after pointing the page correctly I am getting NoReverseMatch Found error on apps main page which haves no connection to it.

This is urls.py of main Project urls.py

urlpatterns = [
    path('', views.home, name="home"),
    path('admin/', admin.site.urls),
    path('teacher/', include('teacher.urls')),
    path('student/', include('student.urls')),
]

This is urls.py for respective app which is teacher urls.py

urlpatterns = [
    path('', views.index, name="index"),
    path(r'^detailed/(?P<reportid>\d{0,4})/$', views.detailed, name="detailed"),
]

I am also including views.py as error is pointing at view.py views.py

def index(request):
    return render(request, 'teacher/report.html')
def detailed(request, reportid):
    weeklyr = wreport.objects.all()
    dailyr = dreport.objects.all()
    split = SplitOfWeek.objects.all()

    return render(request, 'teacher/detailed.html')

I have tried adding r'^teacher/$' at main urls.py and r'^$' at urls.py of teacher app but after adding it shows there is url found for teacher.

This is the detailed error message: Reverse for 'detailed' with no arguments not found. 1 pattern(s) tried: ['teacher/\\^detailed/\\(\\?P(?P<reportid>[^/]+)\\\\d\\{0,4\\}\\)/\\$$']

Sahil Mohile
  • 99
  • 1
  • 9

2 Answers2

0

You shouldn't use regexes with path

A simple fix would be to do:

urlpatterns = [
    path('', views.index, name="index"),
    path('detailed/<int:reportid>/', views.detailed, name="detailed"),
]

However this would allow any number for reportid. If you really to limit the length to four characters, then you could use the regex with re_path:

urlpatterns = [
    path('', views.index, name="index"),
    re_path(r'^detailed/(?P<reportid>\d{1,4})/$', views.detailed, name="detailed"),
]

Another option would be to register a custom path converter for reportid.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • using isn't helping it creates same type of error `Reverse for 'detailed' with no arguments not found. 1 pattern(s) tried: ['teacher/detailed/(?P[0-9]+)/$'] ` – Sahil Mohile Aug 16 '20 at 18:22
  • It is helping - your code that used a regex with `path()` was never going to work. Now you need to fix the next part of the error. *with no arguments not found* means that you aren't providing the report id when you call `reverse()` or use the `{% url %}` tag. – Alasdair Aug 16 '20 at 18:52
  • Thanks for helping me understand issue. I fixed it by giving a default value to function as `def detailed(request, reportid=None):` and creating a additional url for detailed page like `path('detailed/', views.detailed, name="detailed")`. If you would like you can update your question with this answer so I can accept. And upvote my question if you think this may be useful to others – Sahil Mohile Aug 16 '20 at 19:13
0

After help in understanding issue from @Alasdair i found that adding a default value for reportid in view function is solution.

so views.py changed to this

def detailed(request, reportid=None):

Also to go to that default value I added url for default view.

So urls.py changed to this

urlpatterns = [
    path('', views.index, name="index"),
    path('detailed/<int:reportid>/', views.detailed, name="detailed"),
    path('detailed/', views.detailed, name="detailed"),
]
Sahil Mohile
  • 99
  • 1
  • 9