2016-12-28 52 views
0

我遇到了问题,我需要的是将零件号保存到数据库表中。所以每次用户进入SOSS时都应该保存在我的表格中。这是我的代码,但没有保存任何东西,不知道我做错了什么。Django将数据保存到数据库中

manifiestos.html

          <form action="{% url 'manifiestos' %}" method="post"> {% csrf_token %} 
               <p><label for="date"> Date:</label> <input type="text" name="date" value={% now "Y-m-d" %} /> </p> 
               <p><label for="soss"> SOSS:</label> <input type="text" name="soss" id="soss" /> </p> 
               <input type="submit" value="Submit" /> 
             </form> 

models.py

class manifiestos_bts(models.Model): 
    soss = models.CharField(max_length=50) 
    date = models.DateTimeField(null=True, blank=True) 
    user = models.CharField(max_length=50) 

forms.py

class ManifiestosForm(forms.Form): 
    soss = forms.CharField() 
    date = forms.DateTimeField() 
    user = forms.CharField() 

个html_views

@login_required(login_url='/msr/login') 
def manifiestos(request): 
    if request.method == 'POST': 

     form = ManifiestosForm(request.POST) 
     if form.is_valid(): 
      soss = request.POST.get('soss', '') 
      date = request.POST.get('date', '') 
      manifiestos_obj = manifiestos_bts(soss= soss, date= date) 
      manifiestos_obj.save() 

      return HttpResponseRedirect(reverse('manifiestos')) 
    else: 
     form = ManifiestosForm() 

    return render(request, 'manifiestos.html', {'form': form}) 

urls.py

url(r'^manifiestos$', html_views.manifiestos, name='manifiestos'), 

感谢您的时间:) 如果您需要更多的细节只是让我知道。

+0

中删除它是什么结果?你为'user'传递了什么?当你已经有'form'的时候,你为什么使用request.POST ['soss']'? – kia

回答

2

您的form.is_valid()将失败,因为您没有从模板传递用户。将其从ManifiestosForm中移除或将其从manifiestos.html

+0

这就是为什么不传递数据?我应该删除用户或添加它呢?让我试试 – Deluq

+0

它工作!谢谢 :) – Deluq