django: taking input and showing output in the same page

后端 未结 4 1402
Happy的楠姐
Happy的楠姐 2020-12-25 08:15

I am quite new to django and struggling to do something very simple. I have a ModelForm for the following model:

class Queries(models.Model):
         


        
4条回答
  •  醉酒成梦
    2020-12-25 08:54

    views.py

    def index(request):
        questions=None
        if request.GET.get('search'):
            search = request.GET.get('search')
            questions = Queries.objects.filter(query__icontains=search)
    
            name = request.GET.get('name')
            query = Queries.object.create(query=search, user_id=name)
            query.save()
    
        return render(request, 'basicapp/index.html',{
            'questions': questions,
        })
    

    html

    Question:
    Name:


    {% for question in questions %}

    {{question}}

    {% endfor %}

提交回复
热议问题