Django - 我无法通过表单在db中发布数据。为什么?

时间:2017-11-15 07:30:00

标签: django django-models django-forms django-templates

forms.py:

from django import forms
from django.forms import ModelForm
from django.utils import timezone

from .models import Book

class BookCreateForm(forms.Form):
   title = forms.CharField(required=True)
   author = forms.CharField(required=True)
   description = forms.CharField(widget=forms.Textarea)
   genre = forms.CharField(required=True)
   book_image = forms.ImageField(required=True)
   publishedDate = 
   forms.DateField(widget=forms.widgets.DateInput(format="%m/%d/%Y"))
   price =forms.DecimalField(decimal_places=2,max_digits=8)
   stock = forms.DecimalField(decimal_places=2, max_digits=8)

models.py:

from django.db import models
from django.utils import timezone

# Create your models here.

class Book(models.Model):
    title         = models.CharField(max_length=250)
    author        = models.CharField(max_length=250)
    description   = models.TextField()
    genre         = models.CharField(max_length=60)
    book_image    = models.ImageField()
    publishedDate = models.DateField(default=timezone.now)
    price         = models.DecimalField(decimal_places=2, max_digits=8)
    stock         = models.DecimalField(decimal_places=2, max_digits=8)


class Quote(models.Model):
    quote_name = models.TextField()
    quote_author = models.CharField(max_length=250)  

views.py:

from django.shortcuts import render, get_object_or_404
from django.http import  HttpResponse,HttpResponseRedirect
from django.views.generic import TemplateView,ListView,DetailView
from .models import Book
from .forms import BookCreateForm

# Create your views here.

def book_createview(request):
    template_name='bookstores/addbook.html'
    # print (request.GET)
    # print (request.POST)
    if request.method == "GET":
         print ("Get ")
         print(request.GET)
    if request.method == "POST":
         print("post data")
         print (request.POST)
    formed=BookCreateForm()
    form = BookCreateForm(request.POST)

if form.is_valid():
    obj =Book.objects.create(
        title=form.cleaned_data.get('title'),
        author=form.cleaned_data.get('author'),
        description=form.cleaned_data.get('description'),
        genre=form.cleaned_data.get('genre'),
        book_image = form.cleaned_data.get('book_image'),
        publishedDate = form.cleaned_data.get('publishedData'),
        price = form.cleaned_data.get('price'),
        stock = form.cleaned_data.get('stock')
    )
if form.errors:
    print (form.errors)

    #return  HttpResponseRedirect("/store/")

context ={}
return render(request,template_name,context) 

TemplateView:

{% extends "home.html" %}
{% block head_title %}Adding book || {{ block.super }} {% endblock 
 head_title %}
{% block content %}
<p><i>tell us about the book you liked</i></p>
<div class ="form-group">
<form method="POST"> {% csrf_token %}
<input type="text" name ='title' placeholder="Title"><br>
<input type="text" name ='author' placeholder="author"><br>
<input type="text" name ='description' placeholder="description"><br>
<input type="text" name ='genre' placeholder="genre"><br>
<input type="text" name ='book_image' placeholder="book_image"><br>
<input type="text" name ='price' placeholder="price"><br>
<input type="text" name ='stockk' placeholder="stock"><br>


<button type="submit">Save</button>
</form>
</div>
{% endblock  %}

我得到的错误是:

  

获取

  • title
  • 此字段为   
  • 作者
  • 此字段   是必需的。
  • description
  • 此字段为   
  • genre
  • 此字段   是必需的。
  • book_image
  • 此字段为   
  • publishedDate
  • 此字段为   
  • 价格
  • 此字段   是必需的。
  • stock
  • 这个   字段是必需的。
  • [15 / Nov / 2017 12:38:27]“GET   / addbook / HTTP / 1.1“200 2375发布数据https://images.gr-assets.com/books/1506565784l/30835567.jpg'],   'price':['1045.36'],'stockk':['40']}&gt;
  • book_image
  • 此字段   是必需的。
  • publishedDate
  • 此字段为   必需的。
  • 股票
  • 1 个答案:

    答案 0 :(得分:1)

    有两个问题:

    1. 当您发出GET请求时,您正在创建Book对象。你不应该这样做。
    2. 当你提出POST请求时,
    3. 一个。 book_image 键未来,您可以在帖子数据中看到。

      湾来自 stock 的表格将来自 stock 。打字错误。

      ℃。 publishedDate 不会通过表单发送。