Django: 反向 'add_review' 没有找到参数 '('',)'。尝试了 1 种模式:['movies/addreview/(?P<id>[0-9]+)/\Z']
Django: Reverse for 'add_review' with arguments '('',)' not found. 1 pattern(s) tried: ['movies/addreview/(?P<id>[0-9]+)/\\Z']
我想在movie_detail.html
中添加评论功能,但我不知道如何查询评论并映射到movie_detail
。它returns反向为'add_review'带有参数 '('',)' 未找到错误。
我的url.py:
urlpatterns = [
path('', MovieList.as_view(), name='movie_list'),
path('<int:pk>', MovieDetail.as_view(), name='movie_detail'),
path('search/', MovieSearch.as_view(), name='movie_search'),
path('addreview/<int:id>/', views.add_review, name='add_review'),
]
我的model.py:
class Movie(models.Model):
title = models.CharField(max_length=200)
actors = models.CharField(max_length=500, null=True)
poster_url = models.CharField(max_length=200)
director = models.CharField(max_length=200, default='')
score = models.FloatField()
genres = models.CharField(max_length=200)
language = models.CharField(max_length=200, default='English')
durations = models.IntegerField(default=0)
regions = models.CharField(max_length=200, default='')
release_date = models.DateField(default=timezone.now)
description = models.TextField(max_length=1000, default='')
year = models.IntegerField(default=2000)
views_count = models.IntegerField(default=0)
created = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
def genres_as_list(self):
genre_list = self.genres.split('/')
return genre_list
def actors_as_list(self):
return self.actors.split('/')
class Review(models.Model):
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
comments = models.TextField(max_length=1000)
rating = models.FloatField(default=0)
data_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
我的view.py:
class MovieDetail(DetailView):
model = Movie
template_name = "main/movie_detail.html"
def get_object(self):
object = super().get_object()
object.views_count += 1
object.save()
return object
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['links'] = MovieLinks.objects.filter(movie=self.get_object())
context['related_movies'] = Movie.objects.filter(genres__in=self.get_object().genres_as_list()).order_by(
'created')[0:6]
# context['reviews'] = Review.objects.filter(id=self.kwargs['pk'])
return context
class MovieSearch(ListView):
model = Movie
paginate_by = 20
template_name = "main/movie_list.html"
def get_queryset(self):
query = self.request.GET.get('query')
if query:
object_list = self.model.objects.filter(title__icontains=query)
else:
object_list = self.model.objects.none()
return object_list
@login_required
def add_review(request, id):
movie = Movie.objects.get(id=id)
if request.method != "POST":
form = ReviewForm(request.POST or None)
if form.is_valid():
data = form.save(commit=False)
data.comment = request.POST["comment"]
data.rating = request.POST["rating"]
data.user_id = request.user.pk
data.movie_id = movie.pk
data.save()
return redirect("main:movie_detail", id)
else:
form = ReviewForm()
context = {"form": form}
return render(request, 'main/movie_detail.html', context)
我的movies_detail.html
评论部分:
<section class="comments">
<h3>Comment</h3>
<div class="card">
<div class="card-body">
<h3 class="text-center">Add Review</h3>
<form method="POST" action="{% url 'main:add_review' movie.id%}">
{% csrf_token %}
<label for="comment">Review</label>
<textarea name="comment" id="comment" cols="30" rows="5" class="form-control"></textarea>
<label for="rating">Rating</label>
<input type="text" name="rating" class="form-control">
<br>
<input type="submit" class="btn btn-danger" value="Add Review">
</form>
</div>
</div>
我想添加评论功能,我在 view.py 中编写了 add_review
代码 tutorial.But 我不知道如何修复它。
备注
- 当您想要 post 数据呈现的视图时,您无需指定操作,而是保留为
action=""
您没有将电影对象传递给模板,因此 movie.id
求值为空字符串,这会导致您看到的异常。
试试这个:
@login_required
def add_review(request, id):
movie = Movie.objects.get(id=id)
...
context = {"form": form, "movie": movie}
return render(request, 'main/movie_detail.html', context)
编辑:或者,您可以像 shah sawood 在他的回答中指出的那样设置 action=""
。
附带说明:欢迎来到 Stack Overflow!下次,请 post 完整的堆栈跟踪,这会增加您获得问题答案的机会:)
您收到错误是因为您没有将电影传递给 add_review 的最后两行中的上下文。您需要将其更新为
context = {"form": form, "movie": movie}
return render(request, 'main/movie_detail.html', context)
我想在movie_detail.html
中添加评论功能,但我不知道如何查询评论并映射到movie_detail
。它returns反向为'add_review'带有参数 '('',)' 未找到错误。
我的url.py:
urlpatterns = [
path('', MovieList.as_view(), name='movie_list'),
path('<int:pk>', MovieDetail.as_view(), name='movie_detail'),
path('search/', MovieSearch.as_view(), name='movie_search'),
path('addreview/<int:id>/', views.add_review, name='add_review'),
]
我的model.py:
class Movie(models.Model):
title = models.CharField(max_length=200)
actors = models.CharField(max_length=500, null=True)
poster_url = models.CharField(max_length=200)
director = models.CharField(max_length=200, default='')
score = models.FloatField()
genres = models.CharField(max_length=200)
language = models.CharField(max_length=200, default='English')
durations = models.IntegerField(default=0)
regions = models.CharField(max_length=200, default='')
release_date = models.DateField(default=timezone.now)
description = models.TextField(max_length=1000, default='')
year = models.IntegerField(default=2000)
views_count = models.IntegerField(default=0)
created = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
def genres_as_list(self):
genre_list = self.genres.split('/')
return genre_list
def actors_as_list(self):
return self.actors.split('/')
class Review(models.Model):
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
comments = models.TextField(max_length=1000)
rating = models.FloatField(default=0)
data_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
我的view.py:
class MovieDetail(DetailView):
model = Movie
template_name = "main/movie_detail.html"
def get_object(self):
object = super().get_object()
object.views_count += 1
object.save()
return object
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['links'] = MovieLinks.objects.filter(movie=self.get_object())
context['related_movies'] = Movie.objects.filter(genres__in=self.get_object().genres_as_list()).order_by(
'created')[0:6]
# context['reviews'] = Review.objects.filter(id=self.kwargs['pk'])
return context
class MovieSearch(ListView):
model = Movie
paginate_by = 20
template_name = "main/movie_list.html"
def get_queryset(self):
query = self.request.GET.get('query')
if query:
object_list = self.model.objects.filter(title__icontains=query)
else:
object_list = self.model.objects.none()
return object_list
@login_required
def add_review(request, id):
movie = Movie.objects.get(id=id)
if request.method != "POST":
form = ReviewForm(request.POST or None)
if form.is_valid():
data = form.save(commit=False)
data.comment = request.POST["comment"]
data.rating = request.POST["rating"]
data.user_id = request.user.pk
data.movie_id = movie.pk
data.save()
return redirect("main:movie_detail", id)
else:
form = ReviewForm()
context = {"form": form}
return render(request, 'main/movie_detail.html', context)
我的movies_detail.html
评论部分:
<section class="comments">
<h3>Comment</h3>
<div class="card">
<div class="card-body">
<h3 class="text-center">Add Review</h3>
<form method="POST" action="{% url 'main:add_review' movie.id%}">
{% csrf_token %}
<label for="comment">Review</label>
<textarea name="comment" id="comment" cols="30" rows="5" class="form-control"></textarea>
<label for="rating">Rating</label>
<input type="text" name="rating" class="form-control">
<br>
<input type="submit" class="btn btn-danger" value="Add Review">
</form>
</div>
</div>
我想添加评论功能,我在 view.py 中编写了 add_review
代码 tutorial.But 我不知道如何修复它。
备注
- 当您想要 post 数据呈现的视图时,您无需指定操作,而是保留为
action=""
您没有将电影对象传递给模板,因此 movie.id
求值为空字符串,这会导致您看到的异常。
试试这个:
@login_required
def add_review(request, id):
movie = Movie.objects.get(id=id)
...
context = {"form": form, "movie": movie}
return render(request, 'main/movie_detail.html', context)
编辑:或者,您可以像 shah sawood 在他的回答中指出的那样设置 action=""
。
附带说明:欢迎来到 Stack Overflow!下次,请 post 完整的堆栈跟踪,这会增加您获得问题答案的机会:)
您收到错误是因为您没有将电影传递给 add_review 的最后两行中的上下文。您需要将其更新为
context = {"form": form, "movie": movie}
return render(request, 'main/movie_detail.html', context)