【问题标题】:Reverse for 'action' with arguments '('',)' not found未找到带有参数“(”,)”的“操作”的反向
【发布时间】:2021-03-13 15:38:38
【问题描述】:

我的错误似乎发生在其他人身上,但我仍然不知道我做错了什么,所以请大家帮忙。你能用我的代码支持我吗?一旦我尝试使用带有动作"{% url 'mtg:action' card.name %}" 的表单元素,我就会收到错误Reverse for 'action' with arguments '('',)' not found
编辑:我想补充一下,卡名应该由用户编写,因此在加载页面时它不可用。
提前致谢!

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>

编辑:根据要求添加错误日志:

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:\Users\Sajemiur\PycharmProjects\MtG_reader\mtg_django\mtg\templates\mtg\index.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:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Sajemiur\PycharmProjects\MtG_reader\mtg_django\mtg\views.py", line 14, in index
    return render(request, 'mtg/index.html', context)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\template\defaulttags.py", line 446, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\urls\base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Users\Sajemiur\anaconda3\envs\mtgenv\lib\site-packages\django\urls\resolvers.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$']

【问题讨论】:

  • 可以贴一下申请的日志吗?
  • card.name 为空。您可以尝试打印(card.name)以确保
  • 那我在哪里可以打印出来看看,或者怎么填呢?

标签: python-3.x django


【解决方案1】:

您使用了{% if lookup_card %},但您在视图中传递了card_lookup

如果你只想要卡片的名称而不是从卡片对象context = {'cards': Card.objects.values(name)}中获取名称

【讨论】:

  • 我正在同时编辑代码,我已经更正了这一行,如果我禁用表单元素,它可以正常工作。
猜你喜欢
  • 2020-05-11
  • 2012-07-08
  • 2023-04-03
  • 2021-03-21
  • 2015-02-18
  • 2021-11-09
  • 2019-09-14
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多