Django-不是注册的名称空间 [英] Django - is not a registered namespace

查看:84
本文介绍了Django-不是注册的名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在django / python中处理表单。




home.html:

 < form action =" {%url'home:submit'%}'" method ='post'> 




views.py:

  def commit(request):
a = request.POST([''initial'])
return render(request,'home / home.html ',{
'error_message': returned
})




urls.py:

  from django.conf.urls从中导入URL 
。导入视图
urlpatterns = [
url(r'^ submit / $',views.submit,name ='submit')
]

当我尝试在浏览器中运行它时出现错误:


NoReverseMatch at / home / u 'home'不是注册的名称空间


,并且是另一条错误消息,指出表单存在问题。

解决方案

您应该只在模板中更改操作网址:

 < form action = {%url'submit'%} method ='post'> 






关于url名称空间... / p>

为了能够使用 home 命名空间调用url,您应该在主urls.py文件行中添加一些内容像:



for Django 1.x:

  url(r '^',include('home.urls',namespace ='home')),

django 2.x和3.x

  path('',include((''home.urls','home') ,namespace ='home'))


I am trying to process a form in django/python using the following code.


home.html:

<form action="{% url 'home:submit' %}" method='post'>


views.py:

def submit(request):
    a = request.POST(['initial'])
    return render(request, 'home/home.html', {
        'error_message': "returned"
    })


urls.py:

from django.conf.urls import url
from . import views
urlpatterns = [
    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 another error message indicating a problem with the form.

解决方案

You should just change you action url in your template:

<form action="{% url 'submit' %} "method='post'>


On the note of url namespaces...

In order to be able to call urls using home namespace you should have in your main urls.py file line something like:

for django 1.x:

url(r'^', include('home.urls', namespace='home')),

for django 2.x and 3.x

path('', include(('home.urls', 'home'), namespace='home'))

这篇关于Django-不是注册的名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆