urls django, NoReverseMatch - django

I have this error and I don't know it's origin :
NoReverseMatch at /PROJETTEST/rec
Reverse for 'myp' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P\w+)$']
url(r'^allp$', allp.as_view(), name="allp"),
url(r'^allp/(?P<slug>[^/]+)$',ficheP.as_view(), name="ficheP"),
url(r'^(?P<namep>\w+)$','myp', name="myp"),
url(r'^(?P<namep>\w+)/members$','members', name="members"),
url(r'^(?P<namep>\w+)/rec','rec', name="rec"),
Gen
Members
rec
I have when I want access to ..//rec..

p.name is an empty string in your template. While myp url requires at least one alphanumeric character as the argument.

Related

How to use redirect with multiple parameters in Django views?

I am trying to redirect to a function from another function in views.
But I am getting following error
NoReverseMatch at /sigma/status1/
Reverse for 'testview' with keyword arguments '{'amount': 1.000, 'stat':'Approved', 'ref': '10917'}' not found. 1 pattern(s) tried: ['sigma\\/status2/(?P<amount>\\d+)/(?P<stat>[a-z][A-Z]+)/(?P<ref>\\d+)/$']
Below is corresponding part of my views.py
return redirect(reverse('testview',kwargs={'amount':1.000,'stat':'Approved','ref':str(res['ref'])}))
def payment_status2(request,amount,stat,ref):
return render(request, 'confirm1.html')
Below is corresponding part of my urls.py
urlpatterns = [
url('status1/', views.payment_status1),
url(r'^status2/(?P<amount>\d+)/(?P<stat>[a-z][A-Z]+)/(?P<ref>\d+)/$', views.payment_status2,name="testview"),
]
Your stat regex is incorrect:
?P<stat>[a-z][A-Z]+
This indicates that the argument should start with a lowercase letter and then followed by one or more uppercase letters (such as aPPROVED). You should change it to:
?P<stat>[a-zA-Z]+
or
?P<stat>\w+

NoReverseMatch exception - redirect with keyword arguments

I am getting a NoReverseMatch error when trying to use Django's redirect, and cannot figure out what I am doing wrong.
In views:
def addConjugatedForm(request):
#update database
return redirect('findConjugatedForms', chapter=ch.chapter, verse=verse.verse, book=b.name)
def findConjugatedForms(request, book, chapter, verse):
#return a view
In urls.py:
url(r'^findConjugatedForms/(?P<book>\w+)/(?P<chapter>\d+)/(?P<verse>\d+)/', 'findConjugatedForms'),
The url works as a url, i.e. "...findConjugatedForms/Romans/3/24" returns a page. But when a form on that page is submitted, it won't redirect back.
I have named arguments in my url (which seems to be the common problem in other questions on Stack Overflow) and the values of the kwargs seem to be correct.
Reverse for 'findConjugatedForms' with arguments '()' and keyword arguments '{'chapter': 3, 'verse': 24, 'book': u'Romans'}' not found. 0 pattern(s) tried: []
What am I missing?
redirect uses reverse internally. You need to define a viewname for the url, or use a Python path or pass a callable object:
url(r'^findConjugatedForms/(?P<book>\w+)/(?P<chapter>\d+)/(?P<verse>\d+)/',
'findConjugatedForms',
name='findConjugatedForms') # Define a name
Besides, add a suffix '$' into the pattern to prevent the url from matching other urls that starts with findConjugatedForms/book/chapter/verse/

Reverse for 'note' with arguments '(1,)' and keyword arguments '{}' not found error in Django

My URL:
url(r'^(?P<task_id>\d+)/note/$', login_required(NoteView.as_view()), name='note'),
After note is successfully saved, I want to redirect to the note url with the task_id thus I am using reverse function:
return HttpResponseRedirect(reverse('website.views.note', args=(task_id,)))
My URL looks like this:
http://localhost:8000/1/note/
I think the reverse works only http://localhost:8000/note/1/ but not on http://localhost:8000/1/note/. How can I make it work?
You have given the URL a specific name - 'note'. That means you can't reverse it as 'website.views.note', but only as 'note'.
reverse('note', kwargs={'task_id':task_id})
Try passing the keyword args as
return HttpResponseRedirect(reverse('website.views.note', kwargs={'task_id':task_id,}))
Write :
return HttpResponseRedirect(reverse('note', kwargs={'task_id':task_id,}))
URL Name Should be unique. Here, "note" should be unique. Just pass "note" in HttpResponseRedirect.

URL with args does not reverse

I have the following url:
urlpatterns += patterns('app_common.views_settings',
url(r'([\w-]+)/(\d+)/settings/$', 'settings', name="configuration_homepage"),
url(r'(?P<short_name>[\w-]+)/(?P<product_id>\d+)/settings/modify/(?P<sim_id>\d+)/$', 'modify_sim', name="modify_sim"),
)
urlpatterns += patterns('app_common.views_operator',
url(r'^operator/$', 'choose_operator', name="choose_operator"),
url(r'^(?P<short_name>[\w-]+)/project/$', 'choose_project', name="choose_project"),
url(r'([\w-]+)/(\d+)/$', 'set_product', name="set_product"),
url(r'^(?P<short_name>[\w-]+)/$', 'set_operator', name="set_operator"),
)
I tried to reverse configuration homepage using:
url = reverse('configuration_homepage', kwargs={short_name, product_id})
return HttpResponseRedirect(url)
Some times it works, but other times if failed with this issue (short_name=OCI and product_id=1)
Exception Type: NoReverseMatch
Exception Value: Reverse for 'configuration_homepage' with arguments '(u'1', u'OCI')' and keyword arguments '{}' not found.
If you guys detect something wrong in my code fell free to tell me... I tried to give name to variable but urls are not found in that case.
Use args instead of kwargs,
url = reverse('configuration_homepage', args=[short_name, product_id])
Your kwargs is wrong, you are passing a set() instead of a dict()
What you (probably) want is:
url = reverse('configuration_homepage',
kwargs={short_name: short_name, product_id: product_id})
This is one of the many reasons why I prefer dict(a=1, b=2) over {a:1, b:2} when possible,

Django : NoReverseMatch error when reverse a url

while redirecting to particular url i am trying to pass a variable (cat) to a view. using the reverse
like :
return HttpResponseRedirect(reverse('show_poll'), args=[cat])
above redirect will go to a following view :
def show_poll(request, cat):
print cat
having url as
url(r'^show/(?P<cat>\w+)/$', 'pollsite.views.show_poll', name="show_poll"),
getting : Reverse for 'show_poll' with arguments '()' and keyword arguments '{}' not found.
what am i missing here ?
Pass args to reverse() as
return HttpResponseRedirect(reverse('show_poll', args=[cat]))
#-----------------------------------------------^ closing bracket moved at the end