如何在django中将数据保存到mysql数据库中?

时间:2014-05-29 14:41:43

标签: mysql django python-2.7

我正在尝试将表单数据保存在mysql数据库中以获取汽车租赁信息,但它没有预先存储,但所以我收到错误。为此我尝试了以下代码。

Models.py

from django.db import models


class car_info(models.Model):

   location = models.CharField(max_length=400)
   from_id = models.CharField(max_length=400)
   to = models.CharField(max_length=400)
   date = models.DateField()
   time = models.TimeField()
   def __str__(self):
      return ' '.join([
        self. ordering,

    ])

view.py

from django.shortcuts import render
from dem.models import car_info

def demo(request):

   n = request.POST.get("loc_id", ' ')
   ad = request.POST.get("from_id", ' ')
   em = request.POST.get("to_id", ' ')
   m = request.POST.get("date_id", ' ')
   ch_id = request.POST.getlist("tm_id" ,' ')

#search = car_info(Name)
queryset1 = car_info(location=n, from_id=ad, to=em, date=m, time=ch_id)

queryset1.save()
data = {
    'queryset1': queryset1,

}

return render(request, 'home.html', data)

home.html的

 <body>
 <form action="/link/" id="frm_id" method="POST">
 {% csrf_token %}
 <h3><marquee>Car rental</marquee></h3>
 <div>
 <table class="center">
 <tr>
 <td>Location
    <select name="loc_id"><option value='pune'>Pune</option>

    </select>
 </td>
 </tr>
 <tr>
    <td>From
    <select name="from_id"><option value='Akurdi'>Akurdi</option>
        <option value='Pashan'>Pashan</option>
        <option value='Baner'>Baner</option>
    </select>
    </td>
    <td>To
    <select name="to_id"><option value='Akurdi'>Akurdi</option>
        <option value='Pashan'>Pashan</option>
        <option value='Baner'>Baner</option>
    </select>
    </td>
  </tr>
  <tr>
  <td>Date
    <input type="date" name="date_id">


 </td>
 <td>Time
    <input type="time" name="tm_id">

 </td>
 </tr>
 <tr>
 <td>
 <input type="submit" value="book a Car" name="sb_id">
 </td>

 </table>
 </div>
 </form>

那么我可以在上面的代码中做些什么更改,以便数据保存在数据库中? 哪里出错了?

收到错误:

exception type:TypeError
exception value:expected string or buffer

我认为这个错误应该在:

 queryset1.save()

0 个答案:

没有答案