我正在研究我的课程项目,但现在我完全陷入困境。我正在创建一个网站来按用户管理锻炼,当我创建新的锻炼实例时,创建锻炼不会重定向到用户的锻炼页面。
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)
url.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")
]
您指定了
app_name = …
,所以它是:
return redirect('myapp:user_workouts')