Python Django - 找不到 '' 的反向。'' 不是有效的视图函数或模式名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45724006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - Reverse for '' not found. '' is not a valid view function or pattern name
提问by Ire
I'm working on my project for a course and I'm totally stuck right now. I'm creating a website to sell products through paypal and the paypal return/cancel pages are not rendering properly. I've followed the examples in my lessons and checked the code a hundred times but obviously there is something I'm missing.
我正在做我的课程项目,现在我完全被困住了。我正在创建一个网站以通过贝宝销售产品,但贝宝退货/取消页面未正确呈现。我已经按照我的课程中的例子检查了代码一百次,但显然我遗漏了一些东西。
I'm getting an error when I go to products, click buy now, login to paypal and then either cancel the purchase or buy the product and return to merchant's page. paypal_return.html and paypal_cancel.html are 2 templates saved under templates/paypal/
当我转到产品时出现错误,单击立即购买,登录贝宝,然后取消购买或购买产品并返回商家页面。paypal_return.html 和 paypal_cancel.html 是保存在 templates/paypal/ 下的 2 个模板
Can anybody see what I'm missing?
有人能看到我错过了什么吗?
Here you can see the full code https://github.com/IreneG5/spss_online
在这里你可以看到完整的代码https://github.com/IreneG5/spss_online
Traceback
追溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/paypal-cancel/
Django Version: 1.11.4
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_forms_bootstrap',
'paypal.standard.ipn',
'home',
'accounts',
'paypal_store',
'products']
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.clickHymaning.XFrameOptionsMiddleware']
Template error:
In template C:\Users\irene\Documents\Full-Stack-Developer\Stream3\Project\spss_online\templates\base.html, error at line 10
Reverse for '' not found. '' is not a valid view function or pattern name. 1 : {% load staticfiles %}
2 : <!DOCTYPE html>
3 : <html lang="en">
4 : <head>
5 : <meta charset="UTF-8">
6 : <title>Buy SPSS Online</title>
7 : <meta name="viewport" content="width=device-width, initial-scale=1.0">
8 : <meta name="description" content="">
9 : <meta name="author" content="">
10 : <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/cerulean/bootstrap.min.css">
11 : <link rel="stylesheet" href="{% static 'css/style.css' %}">
12 :
13 :
14 : </head>
15 : <body>
16 : <div class="container">
17 : <div class='container-fluid'>
18 : <nav id="menu" class="navbar navbar-inverse">
19 : <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
20 : <span class="icon-bar"></span>
Traceback:
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\irene\Documents\Full-Stack-Developer\Stream3\Project\spss_online\paypal_store\views.py" in paypal_cancel
15. return render(request, 'paypal/paypal_cancel.html', args)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\loader.py" in render_to_string
68. return template.render(context, request)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\backends\django.py" in render
66. return self.template.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render
207. return self._render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\loader_tags.py" in render
177. return compiled_parent._render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\loader_tags.py" in render
72. result = block.nodelist.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render
990. bit = node.render_annotated(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\base.py" in render_annotated
957. return self.render(context)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\template\defaulttags.py" in render
458. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Users\irene\virtualenv\spss_online_env\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
497. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /paypal-cancel/
Exception Value: Reverse for '' not found. '' is not a valid view function or pattern name.
回答by Alasdair
When you use the url tag you should use quotes for string literals, for example:
当您使用 url 标签时,您应该对字符串文字使用引号,例如:
{% url 'products' %}
At the moment product
is treated like a variable and evaluates to ''
in the error message.
目前product
被视为一个变量,并''
在错误消息中计算为。
回答by Aditya Mishra
- The syntax for specifying url is
{% url namespace:url_name %}
. So, check if you have added theapp_name
in urls.py. - In my case, I had misspelled the url_name. The urls.py had the following content
path('<int:question_id>/', views.detail, name='question_detail')
whereas the index.html file had the following entry<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
. Notice the incorrect name.
- 指定 url 的语法是
{% url namespace:url_name %}
. 因此,请检查您是否app_name
在 urls.py 中添加了。 - 就我而言,我拼错了 url_name。urls.py 具有以下内容,
path('<int:question_id>/', views.detail, name='question_detail')
而 index.html 文件具有以下条目<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
。请注意不正确的名称。
回答by Dartion
Fix urlpatterns
in urls.py file
修复urlpatterns
urls.py 文件
For example, my app name is "simulator",
例如,我的应用程序名称是“模拟器”,
My URL pattern for login
and logout
looks like
对于我的URL模式login
和logout
模样
urlpatterns = [
...
...
url(r'^login/$', simulator.views.login_view, name="login"),
url(r'^logout/$', simulator.views.logout_view, name="logout"),
...
...
]
回答by ??????? ????
I was receiving the same error when not specifying the app
name before pattern name.
In my case:
app
在模式名称之前没有指定名称时,我收到了同样的错误。就我而言:
app-name
: Blog
app-name
: 博客
pattern-name
: post-delete
pattern-name
: 删除后
reverse_lazy('Blog:post-delete')
worked.
reverse_lazy('Blog:post-delete')
工作。
回答by Varun Sharma
In my case, what I did was a mistake in the url tag in the respective template. So, in my url tag I had something like
就我而言,我所做的是相应模板中 url 标记中的错误。所以,在我的 url 标签中,我有类似的东西
{% url 'polls:details' question.id %}
{% url 'polls:details' question.id %}
while in the views, I had written something like:
在视图中,我写了如下内容:
def details(request, question_id): code here
def details(request, question_id):代码在这里
So, the first thing you might wanna check is whether things are spelled as they shoould be. The next thing then you can do is as the people above have suggested.
因此,您可能想要检查的第一件事是内容是否按应有的方式拼写。接下来你可以做的就是按照上面的人的建议。
回答by Raj More
Give the same name in urls.py
在 urls.py 中给出相同的名称
path('detail/<int:id>', views.detail, name="detail"),
回答by aman sharma
If you dont define name in the path field, usually the error will come.
e.g.: path('crud/',ABC.as_view(),name="crud")
如果在路径字段中没有定义名称,通常会出现错误。例如:path('crud/',ABC.as_view(),name="crud")
回答by Douglas T
On line 10 there's a space between s
and t
. It should be one word: stylesheet
.
在第 10 行,s
和之间有一个空格t
。应该是一个字:stylesheet
。
回答by user3719458
The common error that I have find is when you forget to define
your url in yourapp/urls.py
我发现的常见错误是当您忘记在 yourapp/urls.py