NoReverseMatch error after upgrade Django version - python

I'm newbie in Django and i read many topic here and not found solution for my case. I believe it's easy, but i can't find the solution.
Basically i have the code in my urls.py and the works fine in Django 1.8.4:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'controli2c.view.view_home'),
]
My template file contains
{% url 'controli2c.views.view_home' as home_url%}
<a href="{% url 'controli2c.views.view_home' %}" {% if request.path == home_url %} class="active"{% endif %} >HOME</a>
When i update Django, i get the error "TypeError: view must be a callable or a list/tuple in the case of include()". Then, i change my urls.py code to:
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'views.view_home'),
]
Now, i have the NoReverseMatch when i open the browser (http://localhost:8000):
"Reverse for 'controli2c.view.view_home' not found. 'controli2c.views.view_home' is not a valid view function or pattern name."
In a post in the forum i found:
"The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py. If your URL patterns don't have names, then now is a good time to add one, because reversing with the dotted python path no longer works."
I believe that's my problem. But i don't know what changes i have to do.
Anyone can help me?
Thanks a lot!!

Now you have to pass a callable, so:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', views.view_home),
]
I think it might work now.

I found the solution!
To keep my template file with the same code, i have make these change
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'views.view_home',name='controli2c.views.view_home'),
]
Thanks!

Related

django template just reloads when clicking a link pointing to another template

the template home in my django contains a url pointing to another page but when clicking this link , the home template just reloads here is the code for the urls.py, views.py and home.html
this is urls.py
from django.conf.urls import include, url
from django.contrib import admin
from pizza import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'',views.home,name='home'),
url(r'order/',views.order,name='order'),
]
this is views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'pizza/home.html')
def order(request):
return render(request,'pizza/order.html')
this is home.html
<h3>
Nandias Garder
</h3>
order a pizza
your urls formatting is wrong. you should try this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$',views.home,name='home'),
url(r'^order/',views.order,name='order'),
]
if you notice here I used ^$ to make sure the home url only catches the empty url.
in your urls, the second url (home url) will match with every string. you can go to admin, because it's defined before home url.

NoReverseMatch - not a registered namespace

I am new to Django and I was working at my first project, but I got:
NoReverseMatch with Exception Value: 'hiring_log_app' is not a
registered namespace in base.html
which follows:
href="{% url 'hiring_log_app:index' %}" >Hiring Log
href="{% url 'hiring_log_app:topics' %}" >Topics
I made sure my namespace was correct and I looked at the other topics already opened without figuring out how to solve the issue.
I paste urls.py:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include ('hiring_log_app.urls', namespace='hiring_log_app')),
hiring_log_app/urls.py:
"""Define URL patterns for hiring_log_app"""
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^topics/$', views.topics, name='topics'),
]
views.py:
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for hiring_log_app"""
return render(request, 'hiring_log_app/index.html')
def topics(request):
""" list of topics"""
topics = Topic.objects.raw( "SELECT text FROM HIRING_LOG_APP_TOPIC;")
context = {'topics' : topics}
return render(request, 'hiring_log_app/topics.html', context)
Does anyone know where I am making a mistake?
You have wrongly specified the namespace in urlpatterns. Follow the pattern below:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
Edit:
If you are using Django 2.2.4 then you should use path instead of url since the usage of it is replaced by re_path.
from django.contrib import admin
from django.conf.urls import include
from django.urls import re_path
urlpatterns = [
re_path(r'^admin/', include(admin.site.urls)),
re_path(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))

NoReverseMatch not a registered namespace, django 1.4 issue

I am forced to use django 1.4 to use neo4j, and I am getting a strange message that "accounts" isn't a registered namespace. It most certainly is, right there in the root:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
]
in settings.py:
INSTALLED_APPS = (
...
'accounts',
)
TEMPLATE_DIRS = ( os.path.join(BASE_DIR, 'templates'), )
in project root I have templates/accounts/index.html, which looks right because template_dirs points to that folder. It specifically complains at:
return render(request, 'accounts/index.html', locals())
which isn't returning a namspace, it's returning a template name starting at the templates folder. In case it's relevant, in the accounts app (which is installed), I have:
urls.py-
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('accounts.views',
url(r'^$', 'index', name='index'),
url(r'^register/$', 'register', name='register'),
url(r'^login/$', 'user_login_page', name='login'),
url(r'^logout/$', 'user_logout', name='logout'),
)
Why is something that has a namespace causing NoReverseMatch not a registered namespace?
Add this to top of your base html.
{% load url from future %}
In 1.4 and it seems other older versions, {% url 'accounts:register' %} wasn't allowed...it only takes {% url accounts:register %}

How to deal with my login_required url?

I have:
Linker urls:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$',
include('distributors.urls', namespace='distributors')),
url(r'^accounts/', include('allauth.urls')),
]
App urls:
url(r'^$', views.Index.as_view(), name='index'),
url(r'^links/$', login_required(views.LinkListView.as_view(), name='links'),
in my html i have href="{% url 'distributors:index' %}" and href="{% url 'distributors:links' %}"
Views:
class Index(TemplateView):
template_name = "distributors/index.html"
class LinkListView(ListView):
model = Link
template_name = "distributors/links.html"
context_object_name = 'links'
When I try to enter http://127.0.0.1:8000/ I see The included urlconf 'linker.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Any ideas how to fix this?
Firstly remove the dollar sign when including other url patterns.
url(r'^',
include('distributors.urls', namespace='distributors')),
Secondly, you are missing a closing bracket where you are using login_required.
url(r'^links/$', login_required(views.LinkListView.as_view()), name='links'),

Django NoReverseMatch error, reverse function not work with no arguments

this NoReverseMatch error is driving me nuts. I'm using Django 1.6 and I have checked through my urls and it just doesn't work. Please kindly guide me on this.
I basically want to do something deadly simple, just when I submit a html form, I get the data I want and then redirect to a result page, but it just doesn't work...
Here is my index.html file
<form name="input" action="{% url 'whatspring:sending' %}" method="post">
{% csrf_token %}
Recipient: <input type="text" name="usrname">
<br>
<input type="submit">
</form>
<br>
my view.py
def index(request):
return render(request,'springsend/index.html')
def sending(request):
var = request.POST['usrname']
doSomethinghere()
return HttpResponseRedirect(reverse('whatspring:results'))
def results(request):
return render(request,'springsend/send_results.html')
then my app urls.py
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
)
and the main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^send/', include('springsend.urls', namespace="whatspring")),
)
I have tried to look into the problem and it seems that the reverse function cannot get the name for the reverse url that I want (i.e. 'results' under the namespace 'whatspring'....)Am I missing something something trival? Please kindly help.
Your urls.py (springsend one) doesn't seem to have a url for the sending view, that's probably why {% url 'whatspring:sending' %} can't find it.
Simply change it to
from django.conf.urls import patterns, url
from springsend import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^results/$', views.results, name='results'),
url(r'^sending/$', views.sending, name='sending'), # this line
)
Every accessible view needs a url. The user's browser needs to have some address to send things. If it would just send it to your domain without url, Django would have no way to tell which url is requested. Django does not auto-generate these urls (which is probably good).
(The user himself does not need to know this url; you don't need to place any ` links anywhere.)

Categories

Resources