将发布请求中的数据插入Django中的数据库

时间:2018-12-05 14:51:40

标签: python django django-forms django-views python-requests

我在django中创建了一个应用程序,该应用程序从表格Survey_body中获取问题并将其以模态形式呈现在html文件中,并在提交时发送POST请求。但是,当我将数据传递到具有3个字段:Q_id,answer和browser_name的Answer表时,它将传递Q_id的值,而不传递其他两个字段(answer和browser_name)。谁能帮我解决这个问题。

models.py

class Survey_body(models.Model):
ANSWER_TYPES = (
    ('S', 'Stars'),
    ('E', 'Emoji'),
    ('N', 'Numbers'),
    ('T','Text'),
)
survey_id= models.ForeignKey(Survey,on_delete=models.CASCADE)
q_id= models.PositiveIntegerField(primary_key=True)
quistion= models.CharField(max_length=500)
answer_type= models.CharField(max_length=50,choices= ANSWER_TYPES,default= "")



def __str__(self):
    return str(self.survey_id) + '     ==> body_id=' + str(self.q_id) +" the 
question is: " +self.quistion



class Answer(models.Model):
q_id= models.ForeignKey(Survey_body, on_delete= models.CASCADE, default=1)
answer= models.CharField(max_length=2000)
browser_name = models.TextField(blank=True)

def __str__(self):
    return str(self.q_id) + "    answer is: " + str(self.answer)

views.py

def PostToAnswer(request):
q2= Survey.objects.filter(survey_id__exact=1).get()
q= Survey_body.objects.filter(survey_id__exact=1).all() #to get all records in Survey_bodey
if request.method == 'POST':
    for item in q:
        if(request.POST.get(item.quistion)):

            r_o= sn.get(item.quistion)
            Answer().q_id= item
            Answer().answer= sn.get(item.quistion)
            Answer().browser_name= request.user_agent.browser.family
            Answer().save()
        else:
            print('else')
            return redirect("modal")
return render(request,"base2.html")


def modal(request):
    a= Answer()

    q2= Survey.objects.filter(survey_id__exact=1).get()
    q= Survey_body.objects.filter(survey_id__exact=1).all() #to get all 
records in Survey_bodey
    context={
    'question': q,
    'survey': q2,

    }

   return render(request,"modal.html",context)

modal.html

<body>



<div class="container">
    <!-- <iframe src="/modal"></iframe> -->

  <h2>Modal FEEDBACK Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-default btn-lg" id="myBtn">feedback</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header" style="padding:35px 50px;">
          <button type="button" class="close" data-dismiss="modal">&times;</button>

          <img class=" img-responsive" src="{% static 'images\logo_rj.png' %}">
        </div>
        <div class="modal_bod" style="padding:40px 50px;">
          <form role="modal_bod form" method="POST" action="/answer_post">{% csrf_token %}

            <div class="form-group">
                {% block content %}
                {%for i in question%}

                    {%if i.survey_id == survey %}
                    <label for="{{i.quistion}}">{{i.quistion}}</label>
                    <br>
                        {%if i.answer_type == 'S'%}


                            <div class="star-rating" id="{{i.quistion}}">
                    			<input id="{{i.quistion}}-5" type="radio" name="{{i.quistion}}" value="5">
                    			<label for="{{i.quistion}}-5" title="5 stars">
                    					<i class="active fa fa-star" aria-hidden="true"></i>
                    			</label>
                    			<input id="{{i.quistion}}-4" type="radio" name="{{i.quistion}}" value="4">
                    			<label for="{{i.quistion}}-4" title="4 stars">
                    					<i class="active fa fa-star" aria-hidden="true"></i>
                    			</label>
                    			<input id="{{i.quistion}}-3" type="radio" name="{{i.quistion}}" value="3">
                    			<label for="{{i.quistion}}-3" title="3 stars">
                    					<i class="active fa fa-star" aria-hidden="true"></i>
                    			</label>
                    			<input id="{{i.quistion}}-2" type="radio" name="{{i.quistion}}" value="2">
                    			<label for="{{i.quistion}}-2" title="2 stars">
                    					<i class="active fa fa-star" aria-hidden="true"></i>
                    			</label>
                    			<input id="{{i.quistion}}-1" type="radio" name="{{i.quistion}}" value="1">
                    			<label for="{{i.quistion}}-1" title="1 star">
                    					<i class="active fa fa-star" aria-hidden="true"></i>
                    			</label>
                    		</div>
                        {%elif i.answer_type == 'E'%}
                        <div class="star-rating emoji-rating" id="{{i.quistion}}">
                            <input id="{{i.quistion}}-5" type="radio" name="{{i.quistion}}" value="star-5">
                            <label for="{{i.quistion}}-5" title="5 stars">

                                    <span class="rating5"></span>
                            </label>
                            <input id="{{i.quistion}}-4" type="radio" name="{{i.quistion}}" value="star-4">
                            <label for="{{i.quistion}}-4" title="4 stars">
                                    <span class="rating4"></span>
                            </label>
                            <input id="{{i.quistion}}-3" type="radio" name="{{i.quistion}}" value="star-3">
                            <label for="{{i.quistion}}-3" title="3 stars">
                                    <span class="rating3"></span>
                            </label>
                            <input id="{{i.quistion}}-2" type="radio" name="{{i.quistion}}" value="star-2">
                            <label for="{{i.quistion}}-2" title="2 stars">
                                    <span class="rating2"></span>
                            </label>
                            <input id="{{i.quistion}}-1" type="radio" name="{{i.quistion}}" value="star-1">
                            <label for="{{i.quistion}}-1" title="1 star">
                                    <span class="rating1"></span>
                            </label>
                        </div>
                        {%elif i.answer_type == 'N'%}
                        <div class="star-rating" id="{{i.quistion}}">
                            <input id="{{i.quistion}}-5" type="radio" name="{{i.quistion}}" value="5">
                            <label for="{{i.quistion}}-5" title="5 stars">
                                    <i class="active fa fa-star" aria-hidden="true"></i>
                            </label>
                            <input id="{{i.quistion}}-4" type="radio" name="{{i.quistion}}" value="4">
                            <label for="{{i.quistion}}-4" title="4 stars">
                                    <i class="active fa fa-star" aria-hidden="true"></i>
                            </label>
                            <input id="{{i.quistion}}-3" type="radio" name="{{i.quistion}}" value="3">
                            <label for="{{i.quistion}}-3" title="3 stars">
                                    <i class="active fa fa-star" aria-hidden="true"></i>
                            </label>
                            <input id="{{i.quistion}}-2" type="radio" name="{{i.quistion}}" value="2">
                            <label for="{{i.quistion}}-2" title="2 stars">
                                    <i class="active fa fa-star" aria-hidden="true"></i>
                            </label>
                            <input id="{{i.quistion}}-1" type="radio" name="{{i.quistion}}" value="1">
                            <label for="{{i.quistion}}-1" title="1 star">
                                    <i class="active fa fa-star" aria-hidden="true"></i>
                            </label>
                        </div>



                        {%elif i.answer_type == 'T'%}
                        <input id="{{i.quistion}}-1" type="text" name="{{i.quistion}}" >

                        {%endif%}



                        <br>

                    {%endif%}
                {% endfor %}
                {% endblock %}


            </div>

              <button type="submit" class="sub_btn btn btn-default btn-lg btn-block" value="Submit"> SUBMIT</button>
          </form>
        </div>
        <div class="modal-footer ">
            <div class= "footer_me">
                <h5>Powered by FeedBacker</h5>
            </div>



        </div>
      </div>

    </div>
  </div>
</div>

<script>
$(document).ready(function(){
    $("#myBtn").click(function(){
        $("#myModal").modal();

    });
    $("#myModal").close(function(){
        ("#").open('')
    })
});
</script>

</body>
</html>

0 个答案:

没有答案