Python Django - 不是注册的命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41883254/
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 - is not a registered namespace
提问by Programmerr
I am trying to process a form in django/python using the following code.
我正在尝试使用以下代码在 django/python 中处理表单。
home.html:
主页.html:
<form action="{% url 'home:submit' %}"method='post'>
views.py:
视图.py:
def submit(request):
a = request.POST(['initial'])
return render(request, 'home/home.html', {
'error_message': "returned"
})
urls.py:
网址.py:
url(r'^submit/$', views.submit, name='submit'),
when i try to run it in a browser i get the error:
当我尝试在浏览器中运行它时,出现错误:
NoReverseMatch at /home/ u'home' is not a registered namespace and also i get that there is an error in the form?
NoReverseMatch at /home/ u'home' 不是注册的命名空间,我还知道表单中有错误?
回答by mislavcimpersak
You should just change you action url in your template:
您应该只在模板中更改您的操作网址:
<form action="{% url 'submit' %} "method='post'>
On the note of url namespaces...
关于 url 命名空间的说明...
In order to be able to call urls using home
namespace you should have in your main urls.py file line something like:
为了能够使用home
命名空间调用 urls,您应该在主 urls.py 文件行中包含以下内容:
for django 1.x:
对于 Django 1.x:
url(r'^', include('home.urls', namespace='home')),
for django 2.x and 3.x
对于 django 2.x 和 3.x
path('', include(('home.urls', 'home'), namespace='home'))
回答by vikasvmads
In your main project, open url.py first. Then check, there should be app_name declared at first. If it is not, declare it.
在您的主项目中,首先打开 url.py。然后检查,首先应该声明 app_name 。如果不是,则声明它。
For example, my app name is user info which is declared in url.py
例如,我的应用程序名称是在 url.py 中声明的用户信息
app_name = "userinfo"
urlpatterns = [
url(r'home/', views.home, name='home'),
url(r'register/', views.registration, name='register')
]
回答by muhammed fairoos nm
I also faced the same issue. it is fixed now by adding
我也面临同样的问题。现在通过添加修复它
app_name = "<name of your app>" in app/urls.py
回答by ThorSummoner
if you happen to be nesting include(
s, the namespace compounds, eg. topappname:appname:viewname
如果您碰巧嵌套include(
s,则命名空间会复合,例如。topappname:appname:viewname
回答by azmirfakkri
For Django 3.0, if you're handling your urls within the app and using include
with path
, in your project/urls.py
:
Django的3.0,如果你正在处理的应用程序中的网址,并使用include
与path
在您project/urls.py
:
urlpatterns = [
path(os.getenv('ADMIN_PATH'), admin.site.urls),
path('', include('my_simple_blog.urls', namespace='my_simple_blog')),
path('account/', include('account.urls', namespace='account')),
]
You need to specify namespace
in include
.
您需要namespace
在include
.
And then in your app/urls.py
:
然后在你的app/urls.py
:
app_name = 'account'
urlpatterns = [
path('register/', registration_view, name='register'),
path('logout/', logout_view, name='logout'),
path('login/', login_view, name='login'),
]
The app_name
must match the namespace
you've specified in project/urls.py
.
将app_name
必须符合namespace
您在指定project/urls.py
。
Whenever you're referring to these urls, you need to do it like this:
每当您提到这些网址时,您都需要这样做:
{% url 'namespace:name' %}
If you're using it with redirect
:
如果您使用它redirect
:
return redirect('namespace:name')
回答by user3719458
A common mistake that I always find is when you have some name space in your template, and in YourApp.url you don't have any name space so if you should use name space add in YourApp.url something like this app_name = "blog"
我经常发现的一个常见错误是,当你的模板中有一些命名空间,而在 YourApp.url 中你没有任何命名空间,所以如果你应该使用命名空间,请在 YourApp.url 中添加类似 app_name = "blog 的内容”
then on your temples make sure you add your name space,
so you will have some thing like this "assumption errors are coming from edit.html
"
then on that particular template you will do this"{% url 'blog:vote' pk=post.pk %}"
"{% url 'blog:post_category' category.name %}"
然后在你的寺庙上确保你添加了你的名字空间,所以你会有这样的东西“假设错误来自edit.html
”然后在那个特定的模板上你会这样做"{% url 'blog:vote' pk=post.pk %}"
"{% url 'blog:post_category' category.name %}"
回答by Eyal Israel
tag name must be unique in the urls.py
file inside your application package inside the project! it is important for the template tagging to route whats what and where.
标签名称urls.py
在项目内的应用程序包内的文件中必须是唯一的!模板标记路由whats what和where很重要。
now [1] inside the urls.py
file you need to declare the variable appName
and give it the unique value. for example appName = "myApp";
in your case myHomeAppand [2] also define the urlpatterns list...
现在 [1] 在urls.py
文件中,您需要声明变量appName
并为其指定唯一值。例如appName = "myApp";
在你的情况下myHomeApp和 [2] 也定义了 urlpatterns 列表......
urlpatterns = [..., url(r'^submit/$', views.submit, name='submit'), ...];
in the html file just change the url tag to:
在 html 文件中只需将 url 标记更改为:
<form action="{% url 'myHomeApp:submit' %}" method='post'>
this should sifuce... else just write here and we'll see how to continue on
这应该很重要...否则就写在这里,我们将看到如何继续