1 回答

TA貢獻2065條經驗 獲得超14個贊
該函數get_context_data僅用于為上下文構建數據,不處理 ajax 請求。您需要拆分您的函數以提供對 GET 數據的處理
結構示例
class PostDetailView(DetailView):
model = Post
template_name = "blog/post_detail.html" # <app>/<model>_<viewtype>.html
def get_context_data(self, *args, **kwargs):
[...]
return context
def get(self, request, *args, **kwargs):
if self.request.is_ajax():
context = self.get_context_data(self, *args, **kwargs)
html = render_to_string('blog/comments.html', context, request=self.request)
return JsonResponse({'form': html})
[...]
def post(self, request, *args, **kwargs):
[...]
添加回答
舉報