Having trouble using reverse() - python

It feels like this is a simple problem but I am obviously missing something.
url = reverse('specific', args=(var.pk,))
print(url)
The error message i get is:
Reverse for 'specific' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Here are my urls:
For myapp level
urlpatterns = [
... some stuff ...
url(r'^specific/(?P<var_id>[0-9]+)/$', views.specific, name='specific'),
]
And these are for the project level
urlpatterns = [
url(r'^$', 'myapp.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls', namespace="TestData")),
]
Feels like I'm missing something simple but I am new at this

You are using namespace in you project urls, namespace="TestData", so you also have to provide it in your urls:
url = reverse('TestData:specific', args=[str(var.pk)])

Related

How can I refer to other urls' captured values in Django?

So, I am writing code with Python and Django (and other web dev requirements).
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path('wiki/<str:name>/', views.info, name="info"),
path("create", views.create, name="create"),
path("results", views.search, name="search"),
path("wiki/<str:name>/edit", views.edit, name="edit")
]
The problem I am facing is that in path("wiki/<str:name>/edit", views.edit, name="edit"), I get an error:
NoReverseMatch at /wiki/Django/>
Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P[^/]+)/edit$']>
And I want to refer to the url in path('wiki/<str:name>/', views.info, name="info")
Please help me fix the error

How to use url tag in django?

I am using Django version 1.10.7. Currently in my html I have:
</span>
My project urls.py has
from django.conf.urls import include,url
from django.contrib import admin
from base import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^team/', include('team.urls'), name='team'),
url(r'^game/', include('game.urls'), name='game'),
url(r'^about/', include('base.urls'), name='about'),
url(r'^$', views.home, name='home'),
]
And in views.py,
from django.shortcuts import render
# Create your views here.
def about(request):
return render(request,'base/about.html',{})
def home(request):
return render(request,'base/home.html',{})
This gives the error:
NoReverseMatch at /
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.10.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How can I fix this?
Try to put your urls in this order:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
url(r'^team/', include('team.urls'), name='team'),
url(r'^game/', include('game.urls'), name='game'),
url(r'^about/', include('base.urls'), name='about'),
]
if works, it means that there is a problem in your other included urls files.

Cannot find view : NoReverseMatch error

I am new to python and django and I am working on a project which is already build and I am just learning to understand it by adding some new views.
Application uses TemplateViews and I created a view which is just like the other views created. The Url pattern is also kind of same to the urls which are working fine. But my new page I created is not working and is giving me the error.
Working urls:
urls.py:
**
url(r'^aboutxyz/$', TemplateView.as_view(template_name="aboutxyz.html"), name='about_xyz'),
url(r'^contactus/$', TemplateView.as_view(template_name="contactus.html"), name='contact_us'),
**
base.html:
**
<li>About XYZ</li>
<li>Contact Us</li>
**
These 2 pages are working fine. But when I add this:
urls.py:
url(r'^aboutxyzsg/$', TemplateView.as_view(template_name="aboutxyzsg.html"), name='about_xyz_sg'),
base.html:
"<li>About XYZ sg</li>"
Here is the code from urls.py file which contains url_patterns:
urlpatterns = patterns('',
url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns(
'',
# zinnia-tinymce
url(r'^tinymce/', include('tinymce.urls')),
url(r'^tinymce/zinnia/', include('zinnia_tinymce.urls')),
# index (home)
url(r'^$', Index.as_view(), name="main_page"),
# all-auth account
url(r'^accounts/', include('allauth.urls')),
# custom user
url(r'^accounts/', include('users.urls')),
# Privacy Policy
url(r'^privacypolicy/$', TemplateView.as_view(template_name="privacypolicy.html"), name='privacy_policy'),
# About XYZ
url(r'^contactus/$', TemplateView.as_view(template_name="contactus.html"), name='contact_us'),
url(r'^aboutxyz/$', TemplateView.as_view(template_name="aboutxyz.html"), name='about_xyz'),
url(r'^aboutxyzsg/$', TemplateView.as_view(template_name="aboutxyzsg.html"), name='about_xyz_sg'),
)
It is not working and giving me error:
My error log shows:
"NoReverseMatch: Reverse for 'about_xyz_sg' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
Please help me I am in deep trouble
P.S. - I tried putting aboutxyzsg url pattern before aboutxyz and still same error

Trying to trace a circular import error in Django

I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django its giving me this error message:
The included URLconf module 'accounts_app' from path\to\myproject\__init__.py 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.
The issue started when i added a new app which has a urls.py like the following
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^signin$', views.signin, name='signin'),
url(r'^signout$', views.signout, name='signout'),
url(r'^signup$', views.signup, name='signup'),
]
My project urls.py has a line which points to the app and looks like the following code
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
My view looks like the following:
from django.shortcuts import render
from django.http import HttpResponse
def signin(request):
return HttpResponse("<p>This the signin view</p>")
def signout(request):
return HttpResponse("<p>This the signout view</p>")
def signup(request):
return HttpResponse("<p>This the signup view</p>")
Can anyone please help me identify were I could possibly be going wrong.
for those who have the same error but still hasn't debugged their code, also check how you typed "urlpatterns"
having it mistyped or with dash/underscore will result to the same error
Try changing
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
to
urlpatterns = [
url(r'^accounts/', include('accounts_app.urls')), # add .urls after app name
]
Those habitual with CamelCased names may face the error as well.
urlpatterns has to be typed exactly as 'urlpatterns'
This will show you error -
urlPatterns = [
path('', views.index, name='index'),
Error -
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from '...\\polls\\urls.py'>' 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.
However, fixing the CamelCase will work -
urlpatterns = [
path('', views.index, name='index'),
]
After 1 hour search it appears that it's wrong speelling it should be : urlpatterns
urlpatterns = [
path('', views.index, name="index")
]
In my case, I got this error because I called the reverse function for a url that required a slug parameter without placing the correct parameter in it.
Once I fixed the reverse function, it was resolved.
In my case I was getting error because I was giving wrong path of dir containing urls. So I changed this
urlpatterns = [
url(r'^user/', include('core.urls'))
]
to this
urlpatterns = [
url(r'^user/', include('core.urls.api'))
]
In my case i was getting this error because i was typing urlpatterns in urls.py to urlpattern.
This error also appears if SomeView doesn't exist in views.py and you do this:
from someapp import views
urlpatterns = [
path('api/someurl/', views.SomeView.as_view(), name="someview"),
]
So make sure all Views you use in urls.py exist in views.py
In my case, I was getting
/SLMS_APP1/urls.py'>' 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.
I made a typo in 'urlpatter'
urlpatter = [
path('',views.index, name='index'),
]
where in correct spelling has to be 'urlpatterns'
urlpatterns = [
path('',views.profile, name='profile'),
]
this error basically occurs when you have, include the app in the main urls.py file and haven't declared urlpattern= [] in-app file.
In my case I changed "reverse" for "reverse_lazy" on my success_url and magially it works.
I changed this...
app_name='users'
urlpatterns=(
path('signup/', SignupView.as_view(),name='signup')
)
to this...
app_name='users'
urlpatterns=(
path('signup/', SignupView.as_view(),name='signup'),
)
notice the last COMMA

named url reverse does not work if url case sensitive

im trying to get my url named, so it can be reversed in my django app.
mysite/urls.py:
urlpatterns = patterns('',
# Examples:
url(r'^$', 'myapp.views.redirect_to_home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),)
myapp/urls.py
urlpatterns = patterns('',
url(r'(?i)^$', views.redirect_to_home),
url(r'(?i)^Login/$', views.home, name="home"),
url(r'(?i)^Logout/$', views.logout_user, name="logout_user"),)
be minded that i had purpusly added the '(?i)^' in the beggining of every regex, to make the urls in-case sensitive.
but, having this string '(?i)^' had made my reverse function to fail saying
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['myapp/(?i)^Login/$']
after trying to remove the (?i)^ , i noticed the reverse succeeds.
What can i do to make my urls in-case sensitive, and still reversable?
Thanks.

Categories

Resources