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.
URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.
reverse() If you need to use something similar to the url template tag in your code, Django provides the following function: reverse (viewname, urlconf=None, args=None, kwargs=None, current_app=None)
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'))
In your main project, open url.py first. Then check, there should be app_name declared at first. If it is not, declare it.
For example, my app name is user info which is declared in url.py
app_name = "userinfo" urlpatterns = [ url(r'home/', views.home, name='home'), url(r'register/', views.registration, name='register') ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With