0

I'm like 3 days trying to find the solution to this problem. I have a dashboard with items, every single item is called board. The dashboard is populated with object (board) that i've stored in my local db. Data like the name of this board is shown in the html page called dashboard.

What i want to do is, when i click on the link named 'Scopri di più' , to be redirected in the board.html populated with the clicked item data.

At the time i'm only getting the error 'NoReverseMatch'.

What am i doing wrong?

  • Have been thinking about this for a while, but could you check if the path is properly included in the root urlpatterns? And can you tell us what the url is generated in the template, or thus when you clicked on the url. – Blackeagle52 Nov 22 '20 at 23:20
  • Just found this question, https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it, very helpful. – Blackeagle52 Nov 22 '20 at 23:24
  • @Blackeagle52 thank you for your answers. The generated url is something like board/idboard where id is the primary key. What do you mean with path properly include in the root urlpatterns? Looking for an answer i added include to use namespace and url_pattern = appname or something like that. Nothing changed – blackJarvis Nov 22 '20 at 23:34
  • I finally see it! `{% url 'board_view' pk=board.id %}`, in your url, board is not a variable, `bn` is. And as I typed this, the answer appeared. – Blackeagle52 Nov 22 '20 at 23:38
  • You are right too, but i still having the same error: Reverse for 'board_view' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['board/(?P[0-9]+)$'] – blackJarvis Nov 22 '20 at 23:48
  • 1
    could you add the generated HTML code to your question? – gph Nov 22 '20 at 23:52
  • How do i find the generated html? – blackJarvis Nov 22 '20 at 23:59

1 Answers1

1

You have wrong loop variable name in <a href="{% url 'board_view' pk=board.id %}">Scopri di più</a>, you should have <a href="{% url 'board_view' pk=bn.id %}">Scopri di più</a>. Your loop variable name is bn not board. And, what happens under the hood, is that, when Django finds variable that doesn’t exist, the template system inserts the value of the engine’s string_if_invalid configuration option, which is set to ''(empty string). And, as your path defines url only for int arguments, the url with String argument is not found.

datosula
  • 1,496
  • 5
  • 10
  • Thank you for the answer, you are right i forgot to change it. Unfortunately i still have the same issue: Reverse for 'board_view' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['board/(?P[0-9]+)$'] – blackJarvis Nov 22 '20 at 23:46
  • Can you share template snippet where you're using the `{% url ... %}` tag? And also `path` definition urls.py? – datosula Nov 22 '20 at 23:53
  • https://getbootstrap.com/docs/4.0/examples/album/ is the template i'm using – blackJarvis Nov 23 '20 at 00:12
  • I believe the problem is with undefined variable in url tag `{% url 'board_view' id=some_variable %}`, if `some_variable` is not defined/provided in context, it default to empty string, end thus not found as match to `path(r'board/', Board_dets_view.as_view() , name='board_view')`, which requires the argument to be int, by defining ``. So, you have to pay attention to what argument is passed to your url tag in your template. – datosula Nov 23 '20 at 00:29
  • And you also may check if variable is ok by temporarily removing the `url` tag at all, and adding some debug text such as `id : "{{ your_variable_which_is_passed_to_url_tag }}"` and look what is output. – datosula Nov 23 '20 at 00:37