0

here is my urls.py

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

i have url named account-bankcreate with an optional argument ... basically if an argument is passed to view its a edit/update operation if not its insert operation

my view starts like this

def bankcreate( requset , info_id = 0 ):
    errors = []
    default = None
    if info_id is not None and int(info_id) > 0 :
        try:
            default = BankAccounts.objects.get(id=info_id , user_id=requset.user.id)
        except BankAccounts.DoesNotExist :
            default = None

and it works fine but in the template when i'm trying to set the form action

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

if there is no argument passed to view i get this error (it works fine when there is an argument):

NoReverseMatch at /account/bankcreate/

Reverse for 'account-bankcreate' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['account/bankcreate/(?:(?P<info_id>[1-9]+)/)?$']

Request Method:     GET
Request URL:    http://localhost:8000/account/bankcreate/
Django Version:     1.9.6
Exception Type:     NoReverseMatch
Exception Value:    

Reverse for 'account-bankcreate' with arguments '('',)' and 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 at line 5
Reverse for 'account-bankcreate' with arguments '('',)' and 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            {%  for e in errors %}
13             <li>  {{ e }} </li>
14            {%  endfor %}
15            </div>

this links works fine

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

this is cousin error

http://localhost:8000/account/bankcreate/
max
  • 3,614
  • 9
  • 59
  • 107
  • Reverse will fail because it will expect that optional parameter and fail to find it. See [this question](http://stackoverflow.com/questions/14351048/django-optional-url-parameters) for alternative approaches. – solarissmoke May 29 '16 at 04:00
  • @solarissmoke well it shouldn't expect parameter since it's optional , what's point of having optional parameter if it's expected anyway ? btw i'm using so called `fancy regex` in that answer – max May 29 '16 at 04:51
  • It shouldn't, but it does. If you read the comments on that question others are reporting that `reverse` fails with the "fancy regex" option. That is why I suggested you try an alternative approach. – solarissmoke May 29 '16 at 04:55

0 Answers0