URLconf无法使用类似的URL

时间:2012-03-16 01:02:06

标签: python django

这是我的URLconf:

urlpatterns = patterns('',
    url(r'^new_player/$', new_player, name="new_player"),
    url(r'^new_player/player_added/$', player_added, name="player_added"),
    url(r'^(\d+)/new_avatar/$', new_avatar, name='new_avatar' ),
    url(r'^(?P<user_name>[-\w]+)/new_game/$', new_game, name='new_game' ),
    url(r'^$', 'django.views.generic.list_detail.object_list', { 'queryset': Player.objects.all(),'extra_context': {'players' : Player.objects.annotate(total_amount_won=Sum('avatar__games__profit'))}}, 'stakeme_player_list'),
    url(r'^(?P<real_name>[\w|\W]+)/', usernames_by_player),
    url(r'^(?P<user_name>[\w|\W]+)/', avatars_by_username),
)

如上所述,url(r'^(?P<real_name>[\w|\W]+)/', usernames_by_player),会显示相关模板,但url(r'^(?P<user_name>[\w|\W]+)/', avatars_by_username),会显示404。

如果我颠倒最后两个网址的顺序,那么404将显示usernames_by_player而不是avatars_by_username

我可以看到,由于类似的网址格式,这可能会导致问题,但有人可以解释原因以及如何解决吗?

1 个答案:

答案 0 :(得分:2)

按照您定义的顺序查找这些网址。所以这一行url(r'^(?P<real_name>[\w|\W]+)/', usernames_by_player)抓取来自请求网址的所有输入。你应该区分这些网址:

url(r'^realname/(?P<real_name>[\w|\W]+)/', usernames_by_player),
url(r'^username/(?P<user_name>[\w|\W]+)/', avatars_by_username),

希望这会导致正确的方向。