0

There is a view for detail:

def get_viewNum(request, numNom):
    md = Nomenclature.objects.get(pk=numNom)
    all_changes_for_md = Changes.objects.filter(numNomenclature_id=md.numNom)

    return render(request,'Sklad/viewNum.html',context = {'nomenclature':md, 'changes':all_changes_for_md})

There is also a view to display the table:

class TableView(ListView):
    model = Nomenclature
    context_object_name = 'nm'    
    template_name = 'Sklad/viewTable.html'

In template, I output a table and I want that when the button is clicked, there is a transition to the detail of the desired nomenclature. My template:

///
{% for item in nm %}
<tr>
    <td>{{item.numNom}}</td>
    <td>{{item.nameNom}}</td>
    <td>{{item.quantity}}</td>
    <td>{{item.numPolk}}</td>
    <td><a href="{% url 'prosmotr'  %}">Просмотр</a></td>
    <td><button>Печать</button></td>
</tr>
{% endfor %}
///

How do I pass the desired numNom to the url detail string? If you use this:

...
 <td><a href="{% url 'prosmotr' item.numNom  %}">Просмотр</a></td>
...

Returns an error:

NoReverseMatch at /table/ Reverse for 'prosmotr' with arguments '('',)' not found. 1 pattern(s) tried: ['news/(?P[^/]+)/\Z']

My urls.py:

urlpatterns = [
    path('', home, name='rashod'),
    path('add_changes/',CreateChanges.as_view(), name = 'add_changes'),
    path('save', save),
    path('inventriz/', inventriz, name='invent'),
    path('inventriz/inventr', inventr, name='add_invent'),
    path('news/<str:numNom>/',get_viewNum, name='prosmotr'),
    path('table/', TableView.as_view(), name = 'viewTable')
]
StimDoker
  • 35
  • 4
  • Does this answer your question - https://stackoverflow.com/questions/1128693/how-do-i-use-a-decimal-number-in-a-django-url-pattern – ilyasbbu Dec 01 '22 at 06:54
  • 1
    `{% url 'prosmotr' item.pk %}`? Your `Nomenclature` model has no `numNom` field / property / method, etc... – Abdul Aziz Barkat Dec 01 '22 at 06:58
  • @AbdulAzizBarkat it does, check out: https://stackoverflow.com/questions/74600988/django-getting-related-objects/74601102#74601102 – nigel239 Dec 01 '22 at 07:08
  • 1
    @nigel239 how would I have known about another question by the OP that had their models? A question should stand on their own without depending on a previous question to create a [mre]. – Abdul Aziz Barkat Dec 01 '22 at 07:12
  • 1
    @nigel239 my assumption is quite safe in this particular case, there are only two possibilities `item.numNom` is actually an empty string, or `item.numNom` doesn't exist so Django automatically replaces it by an empty string. Also if someone is using a variable or method, without it being defined in the SO question, close vote / flag the question since it doesn't have a [mre]. – Abdul Aziz Barkat Dec 01 '22 at 07:20
  • 1
    Also if you have a separate `numNom` field and it doesn't have a `primary_key=True`, why are you filtering by `Nomenclature.objects.get(pk=numNom)`? Is `numNom` just a duplicate of the `pk`? – Abdul Aziz Barkat Dec 01 '22 at 07:22

1 Answers1

0

As your numNom is a CharField so the view should be:

def get_viewNum(request, numNom):
    md = get_object_or_404(Nomenclature, numMom=numNom)
    all_changes_for_md = get_list_or_404(Changes,numNomenclature=md)

    #....

And in the html url:

...
 <td><a href="{% url 'prosmotr' item.numNom  %}">Просмотр</a></td>
...
``
  • numNom is a CharField, does that matter? – nigel239 Dec 01 '22 at 07:10
  • 1
    @nigel239 How can you say that its a `CharField`, the OP didn't share the model, and in the `get_viewNum` view the OP is using `get(pk=numNom)` that means its a integer. –  Dec 01 '22 at 07:22
  • It doesn't mean anything. I can define `integer = "Hello world"`, but that does not make it an int type. I do get the confusion however, and he said it in another question I happened to see. https://stackoverflow.com/q/74600988/18020941 – nigel239 Dec 01 '22 at 07:24
  • 1
    @Dishantsinghal a primary key can actually be any supported field in Django, you just need to set the [primary_key](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field.primary_key) argument on the model field to specify it as the primary key. `pk` is actually a shortcut field so to speak that points to the actual primary key (by default `id` if you don't specify one) – Abdul Aziz Barkat Dec 01 '22 at 07:32
  • 1
    @AbdulAzizBarkat So sir my answer is not right? –  Dec 01 '22 at 07:34
  • 1
    Well my comment is about something else but yes your answer isn't right since the error OP is having doesn't happen in that view. If you see it says "NoReverseMatch at /table/" and `/table/` is running `TableView`, so somewhere in that view there is code that tries to reverse the url by its name but the argument passed is an empty string. – Abdul Aziz Barkat Dec 01 '22 at 07:36