2017-10-21 86 views
0

我正在學習Django,並且一直在試圖使評論表單正常工作的牆上擊敗我的頭。基本上我正在建立的是一個食譜應用程序。這只是練習,但想法是有人發佈配方,其他人可以對此發表評論。我有它基本上工作,但我不知道如何在提交評論後重定向回到配方詳細視圖。Django創建評論查看通過主題pk

如果我在pk中使用硬編碼,那麼我只需要抓住那個pk!

這裏是我的東西:

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 

而且我的意見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) 

你可以看到我在 '10' 已經硬編碼在success_url的PK。這樣做可以工作,但顯然只能重定向到一個配方。

目前我選擇的食譜我想從一個下拉註釋模式這是不理想的評論。

任何幫助,非常感謝!

+0

將表單添加到「RecipeContent」視圖以發佈評論。創建表單並將其添加到上下文中。在你的模板中包含'pk'作爲一個隱藏字段,這樣它就可以被髮布,並可以通過'CommentCreateView'直接訪問。這樣用戶不必使用下拉菜單。 – dirkgroten

回答

0

您的評論需要參考他們鏈接的食譜。我假設你的Comment模型有一個ForeignKeyRecipe

因此,您的CommentCreateView需要額外的屬性,即pkRecipe。它看起來像你的kwargs已經有了這個密鑰,它被傳遞給你的CommentCreateView。因此,在您CommentView,你只需要重寫:

def get_success_url(self): 
    recipe = Recipe.objects.get(pk=self.kwargs.get('pk')) 
    return recipe.get_absolute_url() 

,並在類級別不定義success_url

+0

非常感謝Dirk我非常感謝你的幫助。它的工作現在。乾杯! –