2017-03-09 6 views
0

내가 장고에 새로운 오전 캔트NoReverseMatch 오류 장고 1.10

나는 다음과 같은 오류를 얻을

Reverse for 'todo_list' with arguments '()' and keyword arguments 

'{'cid': 1}' not found. 1 pattern(s) tried: ['todo/(?P<cid>)/'] 
    1 {% extends "base.html" %} 
    2 {% block nav_title %} Company Batches {% endblock nav_title %} 
    3 {% block content %} 
    4  <div class="jumbotron"> 
    5 
    6   {% for obj in object_list %} 
    7    <a href={% url 'todo_list' cid=obj.company.id%} class="href-nostyle"> 
    8     <div class="container"> 
    9      <div class="jumbotron" style="background:white"> 
    10       <div class="text-center"> 
    11        <h1>{{ obj.company }}<br> 
    12         <small>{{ obj.job }}</small> 
    13        </h1> 
    14       </div> 
    15     </div> 
    16     </div> 
    17   </a> 

이 템플릿의 이름은 응용 프로그램에 있습니다 company_batches 내 문제에 대한 해결책을 찾을 수가 내가 HREF

내 URL 태그를 사용하여 할 일 응용 프로그램에 사용자를 탐색을 시도하고하는 것은

입니다 17,451,515,
{% url 'todo_list' cid=obj.company.id%} 

내 주요 urls.py

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^$', home, name='home'), 
    url(r'^batches/', include('company_batches.urls')), 
    url(r'^todo/', include('todo.urls'), name='todo') 
] 

할 일/urls.py

urlpatterns = [ 
    url(r'^$', ToDoCreateView.as_view(), name='todo_create'), 
    url(r'^(?P<cid>)/', ToDoListView.as_view(), name='todo_list'), 
    ] 

관련 views.py 내가 뭘하는지 파악하지 못할

class ToDoListView(ListView,): 
    template_name = 'todo/todo_list.html' 
    def get_context_data(self, *args, **kwargs): 
     context = super(ToDoListView, self).get_context_data(*args, **kwargs) 
     return context 
    def get_queryset(self, cid): 
     return ToDoList.objects.filter(company=self.cid) 

틀린, 약간의 안내는 많이 감사하게 될 것이다

+0

을 사용할 수 있습니다. –

답변

2

여기에 유의해야 할 몇 가지 사항이 있습니다.

정규 표현식, 여기
아마 실제 문제에 적절한 캡처 그룹을 포함하지 않는 url regex에서 cid 캡처. 그것이 ID이기 때문에, 당신은 단지 \d+

url(r'^(?P<cid>\d+)/', ToDoListView.as_view(), name='todo_list'), 

닫는 기호를 포함하지 않는 URL 정규식을
현재 URL을 닫기로 자리를 캡처합니다. /app/<id>/ 뒤에 실제로 URL이 끝나면 $ 달러 기호를 사용하여 정규식을 닫아야합니다. todo 앱 URL을 포함하면서

url(r'^(?P<cid>\d+)/$', ToDoListView.as_view(), name='todo_list'), 

네임 스페이스 사용
하기는 name을 사용하고 있습니다. 네임 스페이스를 올바르게 사용하려면 todo/ url에 이름을 삭제하고 namespace을 포함에 추가해야합니다.

url(r'^todo/', include('todo.urls', namespace='todo')) 

이제 템플릿에서 정규 표현식에있는 당신의`cid` 캡처 그룹이 비어있는 네임 스페이스

{% url 'todo:todo_list' cid=obj.company.id %} 
+0

네, 맞습니다. 정규 표현식이 cid를 올바르게 캡쳐하지 못했습니다. 자세한 답변 주셔서 감사합니다. –

2

정규 표현식이 잘못되었습니다. 그것은 일치시킬 문자가 없습니다. 숫자 PK를 캡처하려는 것 같습니다.

r'^(?P<cid>\d+)/