Reputation: 9682
I am trying to call a view that registers players to a tournament on button click, register them in the view, then essentially refresh the page to reflect their new registration. I have here views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Tournament, TournamentRegistration
def index(request):
tournaments = Tournament.objects.order_by("-start_time")
context = {'tournaments': tournaments}
return render(request, 'tournaments/index.html', context)
def tournament_detail(request, tourney_id):
tournament = Tournament.objects.get(pk=tourney_id)
user = request.user
player_is_registered = TournamentRegistration.player_is_registered(tourney_id, user.id)
print(player_is_registered)
if player_is_registered:
registered = 'yes'
else:
registered = 'no'
context = {'tournament': tournament, 'player_is_registered': registered}
return render(request, 'tournaments/tournament_detail.html', context)
def register_for_tournament(request, tourney_id, player_id):
registration = TournamentRegistration(tournament=tourney_id, player=player_id)
registration.save()
return redirect(tournament_detail, tourney_id=tourney_id)
tournaments/urls.py:
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<tourney_id>[0-9]+)/$', views.tournament_detail, name='tournament_detail'),
url(r'^(?P<tourney_id>[0-9]+)/(?P<player_id>[0-9]+)/$', views.register_for_tournament, name='tournament_registration'),
]
main urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'accounts/', include('accounts.urls')),
url(r'accounts/', include('django.contrib.auth.urls')),
url(r'^tournaments/', include('tournaments.urls')),
url(r'', TemplateView.as_view(template_name='home.html'), name='home'),
]
The template for tournament registration:
<body>
<p>User ID: {{ user.id }}</p>
<p>{{ tournament.name }}</p>
<p>Start time: {{ tournament.start_time }}</p>
<p>Registered: {{ player_is_registered }}</p>
<form action="/tournaments/{{tournament.pk}}/{{user.id}}" method="GET">
<button type="submit">Register</button>
</form>
</body>
I came up with this code based on examples I found online. But to my dismay, instead of refreshing and showing the same page with the player registered, it takes the browser to the url http://127.0.0.1:8000/tournaments/1/1?
which is being caught as the "home" page (the url page with name 'home' in the main urls file).
I checked the tournament detail page again to see if it registered the player, and it did not. So neither of my goals is being met. What am I doing wrong in trying to call this view from a button and redirect to the same page with the new info?
Upvotes: 0
Views: 50
Reputation: 600059
The trouble is two fold. Firstly, you didn't add a slash at the end of your action URL:
<form action="/tournaments/{{tournament.pk}}/{{user.id}}/" ..
Although really you should have used the url
tag here, which would have correctly output the whole URL including the slash:
<form action="{% url 'tournament_registration' tournament.pk user.id %}" ...
And secondly you didn't anchor the home URL, so it matches everything that doesn't match anything else. It should be:
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
Upvotes: 1