如何在django 1.6中使用django模板标签'url'?

时间:2014-03-18 07:57:23

标签: python django django-templates

我使用的是Python 2.7.5,django 1.6.2和bootstrap 3。

我知道自django 1.5以来模板标记{% url %}已更改。我已经阅读了django网站上的documentation for reverse url,但我必须做错事,因为我无法让它发挥作用。

这是我的团队/ urls.py,我使用名为url delete_team_url

from django.conf.urls import patterns, url
import teams.views

urlpatterns = patterns('',
    url(r'^$', teams.views.index, name='teams_index'),
    url(r'^new-team/$', teams.views.index, {'add_new_team':True}, name='teams_index_new_team'),
    url(r'^delete/$', teams.views.delete_team, name='delete_team_url'), 

)

这是项目全球urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

import bbch.views
import auth.views
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^rosetta/', include('rosetta.urls')),
    url(r'^i18n/', include('django.conf.urls.i18n')),

    url(r'^auth/', include('auth.urls', namespace="auth")),
    url(r'^rules/', include('rules.urls', namespace="rules")),
    url(r'^forum/', include('forum.urls', namespace="forum")),
    url(r'^teams/', include('teams.urls', namespace="teams")),

    url(r'^accounts/login/$', auth.views.connection, {'log_required':True}),
    url(r'^home/log_in/$', bbch.views.home, {'log_message':True}, name="home_log_message"),
    url(r'^home/$', bbch.views.home, name="bbch"),
    url(r'^$',  bbch.views.home, name="bbch"),

)

以下是我的观点(我仅使用虚拟id=0进行测试):

def delete_team(request):
    team = teams.models.Team.objects.get(id=0)
    team.delete()

    return render(request, 'teams/index.html', context)

最后这是我的html代码部分,我使用反向网址模板标记:

<div class="row">
    <div class="col-md-2">
        <a href="{% url 'delete_team_url' %}" class="btn btn-danger delete-team"><span class="glyphicon glyphicon-trash"></span> {% trans "Delete team" %}</a>
    </div>
    <div class="col-md-2">
        <a href="#" class="btn btn-info edit-team"><span class="glyphicon glyphicon-pencil"></span> {% trans "Edit team" %}</a>
    </div>
    <div class="col-md-2 col-md-offset-6 btn-right">
        <a href="#" class="btn btn-success add-player"><span class="glyphicon glyphicon-plus"></span> {% trans "Add a player" %}</a>
    </div>
</div>

我收到以下错误:

NoReverseMatch at /teams/
Reverse for 'delete_team_url' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

如果您需要更多信息,请告诉我们!谢谢你的帮助!


编辑:这是teams.views.index(由黄鼠狼询问):

@login_required
def index(request, add_new_team=False) :
    if request.method == 'POST' :

        t_form = teams.forms.CreateTeamForm(request.POST)
        p_form = teams.forms.CreatePlayerForm(request.POST)
        t_errors = t_form.errors
        p_errors = p_form.errors

        if t_form.is_valid() :
            create_new_team = t_form.cleaned_data['new_team']
        else : 
            create_new_team = False

        if p_form.is_valid() :
            add_player = p_form.cleaned_data['new_player']
        else :
            add_player  = False
        #import ipdb; ipdb.set_trace()
        if create_new_team :
            if t_form.is_valid() :

                name = t_form.cleaned_data['name']
                reroll = t_form.cleaned_data['reroll']
                apo = t_form.cleaned_data['apo']
                assistant = t_form.cleaned_data['assistant']
                pompom = t_form.cleaned_data['pompom']
                pop = t_form.cleaned_data['pop']
                value = t_form.cleaned_data['value']
                treasury = t_form.cleaned_data['treasury']

                team = teams.models.Team(
                    name=name,
                    coach=request.user.username,
                    reroll=reroll,
                    apo=apo,
                    assistant=assistant,
                    pompom=pompom,
                    pop=pop,
                    value=value,
                    treasury=treasury,
                    )
                team.save()

                team_set = teams.models.Team.objects.filter(coach=request.user.username)
                player_form = teams.forms.CreatePlayerForm()
                context = {
                    'create_player_form' : player_form,
                    'teams' : team_set,
                    'username' : request.user,
                    'loged_in' : request.user.is_authenticated(),
                }
                return render(request, 'teams/index.html', context)

        elif add_player :
            if p_form.is_valid() :

                name = p_form.cleaned_data['name']
                position = p_form.cleaned_data['position']
                number = p_form.cleaned_data['number']
                team_id = request.POST.get('team_id')
                team = teams.models.Team.objects.get(coach=request.user.username, id=team_id)

                team.player_set.create(name=name, position=position, number=number)
                team.size = team.player_set.count()
                team.save()

                team_set = teams.models.Team.objects.filter(coach=request.user.username)
                player_form = teams.forms.CreatePlayerForm()
                context = {
                    'tab_id': team.id,
                    'create_player_form' : player_form,
                    'teams' : team_set,
                    'username' : request.user,
                    'loged_in' : request.user.is_authenticated(),
                }
                return render(request, 'teams/index.html', context)

        else :
            team_set = teams.models.Team.objects.filter(coach=request.user.username)
            team_form = teams.forms.CreateTeamForm()
            player_form = teams.forms.CreatePlayerForm()

            context = {
            'create_player_form' : player_form,
            'create_team_form' : team_form,
            'alert_form_player' : p_errors,
            'alert_form_team' : t_errors,
            'teams' : team_set,
            'username' : request.user,
            'loged_in' : request.user.is_authenticated(),
            }

            return render(request, 'teams/index.html', context)

    elif request.method == 'GET' :
        team_set = teams.models.Team.objects.filter(coach=request.user.username)
        team_form = teams.forms.CreateTeamForm()
        player_form = teams.forms.CreatePlayerForm()

        context = {
            'add_new_team': add_new_team,
            'create_player_form' : player_form,
            'create_team_form' : team_form,
            'teams' : team_set,
            'username' : request.user,
            'loged_in' : request.user.is_authenticated(),
        }

        return render(request, 'teams/index.html', context)

2 个答案:

答案 0 :(得分:4)

也许是因为您的网址模式末尾缺少$(美元)符号,您会收到此错误。

你应该试试这个:

url(r'^new-team/$' ...),
url (r'^delete/$' ...),

<强>更新

好的,明白了。您在自己的网址中使用了命名空间,这就是您使用{% url %}标记的原因,如下所示:

<a href="{% url 'teams:delete_team_url' %}" ... > ... </a>

答案 1 :(得分:2)

我只是浏览了你的代码,但看起来像{%url&#39; teams:delete_team_url&#39; %}就是你想要的。

(&#34;名称空间&#34;在您的顶级urls.py中成为属于它的视图名称的名称空间;这些名称由&#34;:&#34;从视图名称中分隔)