Jsonresponse 在 CreateView 中不起作用 - Django
Jsonresponse not working in CreateView - Django
我正在尝试通过使用 JsonResponse 在用户开始在字段中键入时实时获取列表,将拥有组 'decoration' 的所有用户放入表单字段。
这里的问题是我没有在表单中获取任何数据作为 Jsonresponse。如果我打印了“标题”,得到了正确的数据,但没有将数据带到表格中...
views.py
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['...']
def get_form(self, form_class=None):
form = super().get_form(form_class)
if 'term' in self.request.GET:
qs = User.objects.filter(groups__name='decoration', username__icontains=self.request.GET.get('term'))
titles = list()
for product in qs:
titles.append(product.username)
return JsonResponse(titles, safe=False)
return form
def form_valid(self, form):
form.instance.author = self.request.user
return redirect(reverse('blog:post-detail', kwargs={'pk': obj.id}))
def form_invalid(self, form):
messages.error(self.request, self.error_message)
form.instance.thumb.checked = True
return super().form_invalid(form)
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
culture = models.CharField(max_length=100)
.HTML
<div class="form-group">
<label>Some text</label>
{% render_field form.culture class="form-control form-st text-body" style="box-shadow: none;font-size:1.5rem;" id="nightclub" placeholder='...' autocomplete="off" %}
</div>
处理自动完成的脚本
<script>
$( function() {
$( "#nightclub" ).autocomplete({
source: '{% url 'blog:post-create' %}'
});
});
</script>
如有帮助将不胜感激!
所以,我找到了解决这个问题的方法。我在 createview class 中定义 get_from() 时做错了,我需要从 class.
外部调用该函数
因此,结果将是:
views.py
@csrf_exempt
def get_autocomplete(request):
qs = User.objects.filter(groups__name='nightclub', first_name__icontains=request.GET.get('term'))
titles = list()
for product in qs:
titles.append(product.first_name)
return JsonResponse(titles, safe=False)
网址:
path('get-autocomplete/', get_autocomplete, name='get-autocomplete'),
html:
<script>
$( function() {
$( "#nightclub" ).autocomplete({
source: '{% url "blog:get-autocomplete" %}'
});
});
</script>
我正在尝试通过使用 JsonResponse 在用户开始在字段中键入时实时获取列表,将拥有组 'decoration' 的所有用户放入表单字段。
这里的问题是我没有在表单中获取任何数据作为 Jsonresponse。如果我打印了“标题”,得到了正确的数据,但没有将数据带到表格中...
views.py
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['...']
def get_form(self, form_class=None):
form = super().get_form(form_class)
if 'term' in self.request.GET:
qs = User.objects.filter(groups__name='decoration', username__icontains=self.request.GET.get('term'))
titles = list()
for product in qs:
titles.append(product.username)
return JsonResponse(titles, safe=False)
return form
def form_valid(self, form):
form.instance.author = self.request.user
return redirect(reverse('blog:post-detail', kwargs={'pk': obj.id}))
def form_invalid(self, form):
messages.error(self.request, self.error_message)
form.instance.thumb.checked = True
return super().form_invalid(form)
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
culture = models.CharField(max_length=100)
.HTML
<div class="form-group">
<label>Some text</label>
{% render_field form.culture class="form-control form-st text-body" style="box-shadow: none;font-size:1.5rem;" id="nightclub" placeholder='...' autocomplete="off" %}
</div>
处理自动完成的脚本
<script>
$( function() {
$( "#nightclub" ).autocomplete({
source: '{% url 'blog:post-create' %}'
});
});
</script>
如有帮助将不胜感激!
所以,我找到了解决这个问题的方法。我在 createview class 中定义 get_from() 时做错了,我需要从 class.
外部调用该函数因此,结果将是: views.py
@csrf_exempt
def get_autocomplete(request):
qs = User.objects.filter(groups__name='nightclub', first_name__icontains=request.GET.get('term'))
titles = list()
for product in qs:
titles.append(product.first_name)
return JsonResponse(titles, safe=False)
网址:
path('get-autocomplete/', get_autocomplete, name='get-autocomplete'),
html:
<script>
$( function() {
$( "#nightclub" ).autocomplete({
source: '{% url "blog:get-autocomplete" %}'
});
});
</script>