I'm working on my project for a course and I'm totally stuck right now. I'm creating a website to manage workouts by user and the create workout do not redirect to the user's workouts page when I create the new workout instance.
views.py
# View to return all user's workouts
def user_workouts(request, user_id):
user = User.objects.get(id=user_id)
print(user)
workouts = get_list_or_404(Workout, user=user.id)
return render(request, "users/workouts.html", {"user": user, "workouts": workouts})
# Create workout view
def add_workout(request, user_id):
user = get_object_or_404(User, id=user_id)
print(user)
print(request._post)
if request.method == "POST":
workout_title = request.POST.get("workout")
reps = request.POST.get("reps")
load = request.POST.get("load")
last_update = request.POST.get("last_update")
workout = Workout(workout=workout_title, reps=reps, load=load, last_update=last_update, user=user)
workout.save()
print(user.id)
return redirect('user_workouts')
context = {"workout": workout}
return render(request, "users/add_workout.html", context=context)
urls.py
from django.urls import path
from . import views
app_name = "myapp"
urlpatterns = [
path("", views.index, name="index"),
path("user/<int:user_id>", views.user_detail, name="user_detail"),
path("user/<int:user_id>/workouts", views.user_workouts, name="user_workouts"),
path("user/<int:user_id>/workouts/<int:workout_id>", views.workout_detail, name="workout_detail"),
path("user/<int:user_id>/workouts/Add Workout", views.open_workout_form, name="open_workout_form"),
path("user/<int:user_id>/workouts/create/", views.add_workout, name="add_workout")
]
You specified an app_name = …
[Django-doc], so it is:
return redirect('myapp:user_workouts')