I cannot find a way in the Django documentation to pass a value between different function views. I would like to create an object in my create_player_view
, capture that new object pk and pass it to scoring_view
. Doing this through the form action field has been unsuccessful as no data is passing between the views. What is a better way to do this?
I want a simple behavior that takes the Match ID created in create_player_view
and passes it for update/use to scoring_view
.
models
class Players(models.Model):
matchID = models.AutoField(primary_key=True)
player1Name = models.CharField(max_length=10)
player2Name = models.CharField(max_length=10)
def __str__(self):
return f'{self.matchID}: {self.player1Name} vs {self.player2Name}'
class Scores(models.Model):
matchID = models.OneToOneField(Players, on_delete=models.CASCADE, related_name='match')
p1_set_1_score = models.IntegerField(default=0)
p1_set_2_score = models.IntegerField(default=0)
p1_set_3_score = models.IntegerField(default=0)
p1_set_4_score = models.IntegerField(default=0)
p2_set_1_score = models.IntegerField(default=0)
p2_set_2_score = models.IntegerField(default=0)
p2_set_3_score = models.IntegerField(default=0)
p2_set_4_score = models.IntegerField(default=0)
def __str__(self):
return f'{self.matchID}: '
views
def create_player_view(request):
"""
allows users to name two
players competing vs one another
"""
if request.method == "POST":
form = PlayerForm(request.POST)
if form.is_valid():
form.save(commit=True)
return redirect('tennis:m_score') #is it possible to pass this view created instance?
else:
message = "Form could not be completed"
return render(request, "create_player.html", {"message":message})
else:
form = PlayerForm()
return render(request, "create_player.html",
{'form': form})
def scoring_view(request):
"""
View allows user to select the participating 2 players
and record their scores per set.
"""
if request.method == "POST":
form = ScoresForm(request.POST )#, instance=player_instance)
if form.is_valid():
form.save(commit=True)
return redirect('tennis:m_results')
else:
form = ScoresForm()
return render(request, "now_playing.html", {'form':form}) #todo add filtering
forms.py
class PlayerForm(ModelForm):
class Meta:
model = Players
exclude = ('matchID',)
labels = {
'playerName':('Player 1 Name', 'Player 2 Name'),
}
class ScoresForm(ModelForm):
class Meta:
model = Scores
fields = "__all__"
###urls.py
from django.urls import path
from tennis import views
app_name="tennis"
urlpatterns = [
path('players/', views.create_player_view, name="c_pl"),
path('scoring/<new_player>/', views.scoring_view, name="m_score"),
path('summary/', views.match_summary_view, name="m_results"),
]
CodePudding user response:
Edit your views:
def create_player_view(request):
""""
allows users to name two
players competing vs one another
"""
if request.method == "POST":
form = PlayerForm(request.POST)
if form.is_valid():
new_player = = form.save(commit=True)
return redirect('tennis:m_score', new_player=new_player) # If you want to pass the pk instead of the object itself write: new_player=new_player.pk
else:
message = "Form could not be completed"
return render(request, "create_player.html", {"message":message})
else:
form = PlayerForm()
return render(request, "create_player.html",
{'form': form})
def scoring_view(request, new_player):
""""
View allows user to select the participating 2 players
and record their scores per set.
"""
if request.method == "POST":
# You can use the value of new_player as your needs
data = request.POST
data['matchID'] = new_player
form = ScoresForm(data)
if form.is_valid():
form.save(commit=True)
return redirect('tennis:m_results')
else:
form = ScoresForm()
return render(request, "now_playing.html", {'form':form}) #todo add filtering