named url reverse does not work if url case sensitive - python

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.

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

Having trouble using reverse()

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)])

Django get reverse url into a variable

I want a reverse url into a variable. I have to apps. one account and another profiles.
my main url
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^account/', include('account.urls', namespace="account")),
url(r'^profiles/', include('profiles.urls', namespace="profiles")),
)
my profiles app url
urlpatterns = patterns('',
url(r'^view/(?P<username>\w+)/$', views.profile, name="profile"),
)
my account app url
urlpatterns = patterns('',
url(r'^message/add_new/$', views.new_message, name='new_message'),
)
my account app view
def new_message(request):
if request.user.is_authenticated():
sender_username = request.user.username
sender_url = reverse('prfiles.views.profile', args=(sender_username,))
but I got 'NoReverseMatch at' error. Please suggest me a better way to solve this.
IMHO, prfiles.views.profile seems like typo.
Fix typo (if it is) or use the URL pattern name instead of the view name.
sender_url = reverse('profiles:profile', args=(sender_username,))
See Reversing namespaced URLs | Django documentation.

Categories

Resources