Reverse for ‘action’ with arguments ‘(”,)’ not found

  django, python-3.x

I have error that seemed to happen to some other people, still I didn’t figure out what am I doing wrong, so asking you all for help. Can you please support me with my code? As soon as I try to use form element with action "{% url 'mtg:action' card.name %}" I receive error Reverse for 'action' with arguments '('',)' not found

EDIT: I’d like to add that card name is suppsed to be written by the user, so its not available as page is loaded.
Thanks in advance!

views.py:

def index(request):
    context = {'cards': Card.objects.all()}
    return render(request, 'mtg/index.html', context)


def lookup(request, card_name):
    context = {'card_lookup': Card.objects.all()}
    return render(request, 'mtg/index.html', context)


def action(request, card_name):
    card = get_object_or_404(Card, pk=card_name)

    # Always return an HttpResponseRedirect after successfully dealing
    # with POST data. This prevents data from being posted twice if a
    # user hits the Back button.
    return HttpResponseRedirect(reverse('mtg:tmpl', args=card_name))

urls.py:

app_name = 'mtg'
urlpatterns = [
    path('', views.index, name="index"),
    path('<str:card_name>/img', views.lookup, name='img'),
    path('<str:card_name>/action', views.action, name='action'),
]

index.html:

<body>
    <form class="wrap" action="{% url 'mtg:action' card.name %}" method="post">
        <input class="searchTerm"/>
        <button class="center" name="search">Search for synergy</button>
        <button class="center" name="lookup">Lookup a card</button>
    </form>
    {% if lookup_card %}<div>{{ card_lookup }}</div>{% endif %}
</body>

EDIT: as requested adding also logs of error:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 3.1.3
Python Version: 3.7.8
Installed Applications:
['mtg.apps.MtgConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:UsersSajemiurPycharmProjectsMtG_readermtg_djangomtgtemplatesmtgindex.html, error at line 45
   Reverse for 'action' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<card_name>[^/]+)/action$']
   35 :     .wrap{
   36 :       width: 30%;
   37 :       position: absolute;
   38 :       top: 30%;
   39 :       left: 50%;
   40 :       transform: translate(-50%, -50%);
   41 :     }
   42 :     </style>
   43 : </head>
   44 : <body>
   45 :     <form class="wrap" action=" {% url 'mtg:action' card.name %} " method="post">
   46 :         <input class="searchTerm"/>
   47 :         <button class="center" name="search">Search for synergy</button>
   48 :         <button class="center" name="lookup">Lookup a card</button>
   49 :         {% if card_lookup %}<img src="{{ card_lookup }}" width="80" height="200"/>{% endif %}
   50 :     </form>
   51 : </body>
   52 : </html>

Traceback (most recent call last):
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangocorehandlersbase.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:UsersSajemiurPycharmProjectsMtG_readermtg_djangomtgviews.py", line 14, in index
    return render(request, 'mtg/index.html', context)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangoshortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplateloader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplatebackendsdjango.py", line 61, in render
    return self.template.render(context)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplatebase.py", line 170, in render
    return self._render(context)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplatebase.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplatebase.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplatebase.py", line 905, in render_annotated
    return self.render(context)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangotemplatedefaulttags.py", line 446, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangourlsbase.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:UsersSajemiuranaconda3envsmtgenvlibsite-packagesdjangourlsresolvers.py", line 685, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'action' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<card_name>[^/]+)/action$']

Source: Python-3x Questions

LEAVE A COMMENT