Django-Tables2 LinkColumn link doesn't work

倖福魔咒の 提交于 2019-12-23 04:31:30

问题


I think I'm missing something obvious here, but I've been stuck at this for so long, I had to post the question.

Link to screenshot

  1. When I click on the link generated in LinkColumn of django-tables2, it redirects back to the same page. Here's my code:

urls.py:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'admin', views.admin, name='admin'),
    url(r'admin/edit/(?P<pk>\d+)/', views.bug_edit, name='bug_edit'),
    url(r'admin/delete/(?P<pk>\d+)/', views.bug_delete, name='bug_delete'),
)

views.py:

def bug_edit(request):
    return HttpResponse('This is an edit page!')

def bug_delete(request):
    return HttpResponse('This is a delete page!')

forms.py:

class SimpleTable(tables.Table):
    edit_link = tables.LinkColumn('bug_edit', args=[A('pk')], 
                verbose_name='Edit', accessor='pk',      attrs={'class':'edit_link'})
    delete_link = tables.LinkColumn('bug_delete', args=[A('pk')], 
                verbose_name='Delete Bug', accessor='pk', attrs={'class':'delete_link'})
    class Meta:
        attrs = {'class' : 'paleblue'}
        model = BugModel

I don't see the HttpResponse on clicking the links, as I should.

Thank you.


回答1:


The problem is your url patterns are missing ^ and $.

Without them, the 'admin' pattern is matching any url with 'admin' in it -- effectively masking the following patterns.

Try using this instead:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^admin/$', views.admin, name='admin'),
    url(r'^admin/edit/(?P<pk>\d+)/$', views.bug_edit, name='bug_edit'),
    url(r'^admin/delete/(?P<pk>\d+)/$', views.bug_delete, name='bug_delete'),
)


来源:https://stackoverflow.com/questions/24868947/django-tables2-linkcolumn-link-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!