please help. got an unexpected keyword argument. Template.

hello! Please help me.

url.py

path("table_details/<int:table>", views.table_details, name="table_details")

views.py

def table_details(request):
    
    table = request.GET.get("table")
    
    return render(request, 'main/table_details.html', {'table': table})

1.html

<a href="{% url 'table_details' 745 %}" class="nav-link text-white">

table_details.html

{% extends 'main/1.html' %}

{%block content%}

{{table}}

{%endblock content%}

error

TypeError at /table_details/745
table_details() got an unexpected keyword argument 'table'
Request Method:	GET
Request URL:	http://127.0.0.1:8000/table_details/745
Django Version:	4.2.1
Exception Type:	TypeError
Exception Value:	
table_details() got an unexpected keyword argument 'table'

this is wrong almost everywhere :slight_smile:

  • a GET parameter is not the same a as url/path keyword argument.
  • the url template call is wrong (missing a kwarg key/name)
  • a GET parameter cannot contain a Table object. You have to fetch the object itself in the view, using the Django ORM.
  • your call request.GET.get("table") implies a url similar to /table_details?table=745… which is not matched by anything
  • do not play with template inheritance until you have 100% mastered the basics above. The way you use it hardly makes any sense.

try this (or similar), but only after understanding your errors:

urls:

path("table_details/<int:table_pk>", views.table_details, name="table_details")

views:

def table_details(request):
    
    try:
        table = Table.objects.get(pk=self.kwargs['table_pk'])
    except Table.DoesNotExist:
        return Http404
    
    return render(request, 'main/table_details.html', {'table': table})

table_details.html

<a href="{% url 'table_details' table_pk=table.pk %}">{{ table.title }}</a> # or whatever other field..
1 Like

BIG THANX :handshake: :slightly_smiling_face:

But I found another solution )

urls

re_path(r'table_details/', views.table_details, name="table_details"),

views

def table_details(request):
table = request.GET.get("table")
    table = int(table)
return render(request, 'main/table_details.html', {'table': table)

html

<a href="{% url 'table_details' %}?table=745">

it also works!)

Yes, it works, but it’s not the “Django-style” way of handling this.

1 Like

Yes, I understand that Django has its own philosophy, I will connect and study. Thank you.

It’s not just a philosophy, it’s also a set of built-in features and safeguards that prevent you from needing to do some of the work.

For example, in the method you have implemented, there is no validation that the value submitted for table is actually a proper integer suitable for use as a key.

However, if you have your url defined as described above:
path("table_details/<int:table_pk>", views.table_details, name="table_details")
then Django will have verified that the entry supplied in the url in that position is an integer. Your view won’t even be called unless it passes the appropriate tests.

1 Like

yes, I agree with you, everything is true, I understand, but I’m a beginner, and I’m still figuring it out, and sometimes a ready-made solution in Django can’t always be understood immediately by a beginner. Therefore, something has to be done in its own way. But in my case, this concept suits me, because there will be no other values except the type of numbers in the link.
Thanks again!

No one denies that there’s a fairly substantial learning curve with Django. That’s one of the reasons why there’s a community of people here to help you along.

You are only impeding your own education and professional development as a Django programmer by ignoring what Django provides for you in favor of an alternative and inferior approach to the well-defined, documented, and standard method of passing parameters into views.

That statement is not accurate. You have no mechanism to prevent a user from changing the value supplied in a url.

Making the assumption that you have any control at all over any data being submitted is the primary cause of most security vulnerabilities. The number one security rule in the development of any web application is that you must not trust any data coming from the browser.

1 Like

Thank you, I will keep in mind!

try this:

<a href="{% url 'table_details' %}?table=hellothere">

how does your code behave with this?

it works without errors during testing.
Do you think this is wrong?

did you click the link?

yes, I followed the link and everything works correctly