我有两个问题:
1)我想在Formset的额外字段中添加一个新播放器,但保存时出现IntegrityError,因为
@Html.EditorFor(model => model.Active, "", new { htmlAttributes = new { @id = "Active", data_toggle = "toggle", data_width = "100%", data_on = "Active", data_off = "Disabled" }})
未设置。我需要团队的价值
team = models.ForeignKey(Team, on_delete=models.CASCADE
但是我不知道将其放在我的views.py中,还是我需要覆盖一些现有功能。
2)当我在每个表单的末尾呈现网站时,出现字符串“ Human ptr:” ...如何删除它?
我有一个用formset_factory制成的表单,呈现如下:
team_area.html
team=request.user.team
该视图由以下位置管理:
views.py
{% extends 'base_layout.html' %}
{% block content %}
<h1>Area Squadra</h1>
<form method="post" action="">
{% csrf_token %}
{{ player_formset.management_form }}
{% for player_form in player_formset %}
{% for field in player_form %}
{{ field.label_tag }} {{ field }}
{% endfor %}
<br>
{% endfor %}
<input type="submit" value="Aggiorna">
</form>
{% endblock %}
播放器型号为:
from django.shortcuts import render, redirect
from skeleton.models import Player
from django.contrib.auth.decorators import login_required
from .forms import PlayerForm
from django.forms import modelformset_factory
# Create your views here.
@login_required(login_url="/accounts/login/")
def team_area(request):
PlayerFormSet = modelformset_factory(Player, fields=('first_name', 'last_name'), extra=1)
if request.method == "POST":
player_formset = PlayerFormSet(
request.POST,
request.FILES,
queryset=Player.objects.all().filter(team=request.user.team),)
if player_formset.is_valid():
player_formset.save()
return redirect('team_area:home')
else:
player_formset = PlayerFormSet(queryset=Player.objects.all().filter(team=request.user.team))
return render(request, 'team_area/team_area.html', {'player_formset': player_formset})