尝试创建页面以使用序列化程序更新我的模型时收到上述错误。无法在edit_team视图中使用pk。有什么想法我要去哪里吗?删除按钮与pk配合正常,不确定为什么不更新。
urls.py
path('teams/', views.TeamInfo.as_view(), name='teams'),
path('delete_team/<int:pk>/', views.delete_team, name='delete_team'),
path('edit_team/<int:pk>/', views.edit_team, name='edit_team'),
teams.html
<div class="team">
<h3>Team Details</h3>
<p>
{% csrf_token %}
{% for info in teams %}
<li>{{ info.club_id }}</li>
<li>{{ info.team_name }}</li>
<li>{{ info.manager_name }}</li>
<form action="{% url 'clubs:delete_team' info.pk %}">
<input type="submit" value="Delete">
</form>
<form action="{% url 'clubs:edit_team' info.pk %}">
<input type="submit" value="Edit">
edit_team.html
{% extends 'club_main_page.html' %}
{% load rest_framework %}
{% load staticfiles %}
{% block body_block %}
<div class="editteam">
<h3>Edit Team</h3>
<form action="{% url 'clubs:edit_team' pk=instance.pk %}" method="POST">
{% csrf_token %}
{% render_form serializer %}
<input type="submit" name="" value="Update">
</form>
</div>
{% endblock %}
views.py
def delete_team(request, pk):
team = Team.objects.filter(pk=pk)
team.delete()
return redirect('clubs:teams')
def edit_team(request, pk):
instance = Team.objects.filter(pk=pk)
if request.method == 'POST':
serializer = TeamSerializer(request.POST, instance=instance)
if serializer.is_valid():
serializer.save()
return redirect('/')
else:
return redirect('/')
else:
serializer = TeamSerializer(instance=instance)
return render(request, 'edit_team.html', {'serializer': serializer})
引用:edit_pitch中的\ clubkit \ clubs \ views.py 返回render(request,'edit_pitch.html',{'serializer':serializer})
答案 0 :(得分:0)
在模板中编写:
<form action="{% url 'clubs:edit_team' pk=instance.pk %}" method="POST">
但是没有 instance
变量,您进入了渲染过程,因此也应该添加此变量,因此:
def edit_team(request, pk):
instance = Team.objects.filter(pk=pk)
if request.method == 'POST':
serializer = TeamSerializer(request.POST, instance=instance)
if serializer.is_valid():
serializer.save()
return redirect('/')
else:
return redirect('/')
else:
serializer = TeamSerializer(instance=instance)
return render(
request,
'edit_team.html',
{'serializer': serializer, 'instance': instance})
但是我不确定,重定向到/
是否是一个好主意,如果serializer
是无效有效的,当然不是。在这种情况下,通常需要再次渲染页面,但会出现错误。因此,make use of forms [Django-doc]可能也是一个更好的主意。
答案 1 :(得分:0)
您没有将PK传递给模板,因此不能用于在其中生成URL。更改视图的最后一行:
return render(request, 'edit_team.html', {''pk': pk, serializer': serializer})
答案 2 :(得分:0)
本来想用串行器解决这个问题。但是只是在这里提供了一种表单解决方案,以防万一有人需要它:
def edit_team(request, pk):
instance = Team.objects.filter(pk=pk).first()
if request.method == 'POST':
form = TeamForm(request.POST, instance=instance)
if form.is_valid():
form.save()
return redirect('clubs:teams')
else:
return redirect('clubs:teams')
else:
form = TeamForm(instance=instance)
return render(request, 'edit_team.html', {'form': form,
'instance': instance})
然后在模板中:
{% extends 'club_main_page.html' %}
{% block head %}
<title>Edit Pitch</title>
{% endblock %}
{% block body_block %}
<div class="container">
<form method="post">
{% csrf_token %}
{{ form.as_p}}
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}