[英]Django Create Comment View pass Subject pk
I'm learning Django and have been beating my head against a wall trying to make a comment form work correctly.我正在学习 Django,并且一直在努力使评论表单正常工作。 Basically what I'm building is a recipe app.
基本上我正在构建的是一个食谱应用程序。 This is just practice but the idea would be that someone posts a recipe and other people can comment on it.
这只是练习,但想法是有人发布食谱,其他人可以对其发表评论。 I have it basically working but I cannot work out how to have the redirect go back to the recipe detail view after the comment has been submitted.
我已经基本正常工作,但我无法弄清楚如何在提交评论后让重定向返回到配方详细信息视图。
If I hard code in the pk it works, I just need to get a hold of that pk!如果我在 pk 中硬编码它可以工作,我只需要掌握那个 pk!
Here is my stuff:这是我的东西:
Portion of Recipes views.py:
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView, View
from django.views.generic.detail import SingleObjectMixin
from django.core.urlresolvers import reverse_lazy
#from comments.models import Comment
from .models import Recipe
from comments.models import Comment
from .forms import RecipeCreateForm
from comments.forms import CommentFormTrial
from comments.views import CommentCreateView
class PersonalRecipeListView(LoginRequiredMixin,ListView):
def get_queryset(self):
return Recipe.objects.filter(owner=self.request.user)
class RecipeDetailView(View):
def get(self, request, *args, **kwargs):
view = RecipeContent.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = CommentCreateView.as_view()
return view(request, *args, **kwargs)
class RecipeContent(DetailView):
model = Recipe
template_name = 'recipes/recipe_detail.html'
context_object_name = 'recipe_data'
def get_context_data(self, *args, **kwargs):
context = super(RecipeContent, self).get_context_data(*args, **kwargs)
print('-------------------')
print(self.kwargs.get('pk'))
qs= Comment.objects.filter(recipe=self.kwargs.get('pk'))
context['comments'] = qs
comment_form = CommentFormTrial
context['comment_form'] = comment_form
return context
And my comments view.py:我的评论 view.py:
from django.shortcuts import render, get_object_or_404
from .models import Comment
from django.http import HttpResponseForbidden
from django.views.generic import ListView, CreateView
from django.core.urlresolvers import reverse_lazy
from .forms import CommentFormTrial
# Create your views here.
class CommentListView(ListView):
model = Comment
context_object_name = 'comments'
template_name='comments/testcomment.html'
class CommentCreateView(CreateView):
model = Comment
form_class = CommentFormTrial
success_url = reverse_lazy('recipes:recipe-detail', kwargs = {'pk':10})
def form_valid(self, form):
form.instance.owner = self.request.user
return super(CommentCreateView, self).form_valid(form)
You can see I have hard coded in '10' for the PK in the success_url.您可以看到我在 success_url 中为 PK 硬编码为“10”。 With this done it works but obviously only redirects to that one recipe.
完成后,它可以工作,但显然只会重定向到那个食谱。
At the moment I'm selecting the recipe I want to comment on from a drop down on the comment modal which isn't ideal either.目前,我正在从评论模式的下拉菜单中选择我想评论的食谱,这也不理想。
Any help is greatly appreciated!非常感谢任何帮助!
Your comments need to have a reference to the Recipe they're linked to.您的评论需要引用它们所链接的食谱。 I'm assuming your
Comment
model has a ForeignKey
to Recipe
?我假设您的
Comment
模型有一个ForeignKey
to Recipe
?
So your CommentCreateView
needs to receive an extra attribute, which is the pk
to the Recipe
.所以你的
CommentCreateView
需要接收一个额外的属性,它是Recipe
的pk
。 It looks like your kwargs
already has that key, which is passed to your CommentCreateView
.看起来您的
kwargs
已经拥有该密钥,该密钥已传递给您的CommentCreateView
。 So in your CommentView
, you just need to override:所以在你的
CommentView
,你只需要覆盖:
def get_success_url(self):
recipe = Recipe.objects.get(pk=self.kwargs.get('pk'))
return recipe.get_absolute_url()
and don't define the success_url
at the class level.并且不要在类级别定义
success_url
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.