python - optional argument in template causing error when there is no argument -


here urls.py

urlpatterns = [     url(r'^bankcreate/(?:(?p<info_id>[1-9]+)/)?$' , views.bankcreate , name='account-bankcreate'), ] 

i have url named account-bankcreate optional argument ... if argument passed view edit/update operation if not insert operation

my view starts

def bankcreate( requset , info_id = 0 ):     errors = []     default = none     if info_id not none , int(info_id) > 0 :         try:             default = bankaccounts.objects.get(id=info_id , user_id=requset.user.id)         except bankaccounts.doesnotexist :             default = none 

and works fine in template when i'm trying set form action

 <form method="post" action="{%  url 'account-bankcreate' default.id %}"> 

if there no argument passed view error (it works fine when there argument):

noreversematch @ /account/bankcreate/  reverse 'account-bankcreate' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['account/bankcreate/(?:(?p<info_id>[1-9]+)/)?$']  request method:     request url:    http://localhost:8000/account/bankcreate/ django version:     1.9.6 exception type:     noreversematch exception value:      reverse 'account-bankcreate' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['account/bankcreate/(?:(?p<info_id>[1-9]+)/)?$'] error during template rendering  in template c:\wamp\www\django\paypal\account\templates\account\bankcreate.html, error @ line 5 reverse 'account-bankcreate' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['account/bankcreate/(?:(?p<info_id>[1-9]+)/)?$'] 1   {%  extends 'master-account.html' %} 2    3   {%  block main %} 4   <div class="col-md-7 col-md-offset-2"> 5       <form method="post" action="{%  url 'account-bankcreate' default.id %}"> 6           {%  csrf_token %} 7    8    9           {% if errors %} 10            <div class="alert alert-danger"> 11            <ul> 12            {%  e in errors %} 13             <li>  {{ e }} </li> 14            {%  endfor %} 15            </div> 

this links works fine

http://localhost:8000/account/bankcreate/1/ 

this cousin error

http://localhost:8000/account/bankcreate/ 


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -