2017-09-20 53 views
0

我正在Django項目上工作,我有一個html網頁,並從網頁我想通過一個Id並通過該ID,更新記錄。我有url設置和html頁面url重定向。我正在傳遞所需的url,但我得到了模式匹配錯誤。我嘗試了一切,出於某種原因,它無法正常工作。有誰知道什麼是oging或可以幫助我找出是什麼導致此錯誤。我會在下方附上所有的信息:Django - url被重定向時不匹配任何模式

這裏是URL鏈接:

url(r'^(?P<source_id>[\w+][0-9]+)/default_source/$', views.setDefaultSource, name='default_source'), 

下面是HTML文件:

<p>{{account.user.username}}, {{ account.source_name }}, {{ account.source_id }}, 
{{ account.status }} 
<a href="{% url 'default_source' account.source_id %}">Make Default</a> 

這裏是views.py:

def setDefaultSource(request, source_id): 
    currentUser = loggedInUser(request) 
    currentSource = Dwolla.object.get(source_id = source_id) 

    update_source = currentSource 
    update_source.status = 2 
    update_source.save() 

    return redirect('home_page') 

這是我得到的錯誤:

NoReverseMatch at /linked_accounts/ 
Reverse for 'default_source' with arguments '('https://api-sandbox.dwolla.com/funding-sources/3021030d-0175-41f1-8bce-4625b8eae0fc',)' not found. 1 pattern(s) tried: ['(?P<source_id>[\\w+][0-9]+)/default_source/$'] 

回答

0

看來你的正則表達式表達不符合您的source_id

[\w+][0-9]+ 

enter image description here

如果3021030d-0175-41f1-8bce-4625b8eae0fc是你SOURCE_ID是什麼樣子,請嘗試:

[\w+-]+ 

或更復雜正則表達式。

+0

源ID是下面的'的https:// api-sandbox.dwolla.com /資金來源/ 3021030d-0175-41f1-8bce-4625b8eae0fc' @zhiyucao –

0

只要改變你的網址模式,

url(r'^(?P<source_id>[\w-]+)/default_source/$', views.setDefaultSource, name='default_source'), 
+0

所以我有以下代碼現在'url(r'^(?P [\ w + - ] +)/ default_source/$',views.setDefaultSource,name ='default_source')',我還在出現以下錯誤:'NoReverseMatch at/linked_accounts/ Reverse'with default'('https://api-sandbox.dwolla.com/funding-sources/3021030d-0175-41f1-8bce-4625b8eae0fc',)'未找到。 1模式嘗試:['(?P [\\ w - ] +)/ default_source/$']不知道爲什麼@zaidfazil –

+0

您是否使用任何命名空間的應用程序url_patterns?請檢查您的project_level'urls.py' ... – zaidfazil

+0

您確定'account.source_id'是'uuid',似乎'account.source_id'是一個uri。 – zaidfazil