gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:57 27 4
gpt4 key购买 nike

我正在使用 Python 2.7.5、django 1.6.2 和 bootstrap 3。

我知道模板标签 {% url %} 自 django 1.5 以来发生了变化。我读过 documentation for reverse url在 django 网站上,但我必须做错事,因为我无法让它工作。

这是我的 teams/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 代码部分,我在其中使用了反向 url 模板标签:

<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(由 weasel 询问):

@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)

最佳答案

可能是因为您的 urlpatterns 末尾缺少 $(美元)符号,您会收到此错误。

你也许应该试试这个:

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

更新:

好的,知道了。您在 url 中使用了命名空间,这就是为什么您必须使用 {% url %} 标签,如下所示:

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

关于python - 如何在 django 1.6 中使用 django 模板标签 'url'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22473390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com