我正在尝试使用基本的Django表单将值插入sqlite数据库。
这些是我的设置
Models.py
class Book(models.Model):
def get_absolute_url(self):
return reverse ('books:detail',kwargs={'pk':self.pk})
def __str__(self):
return self.name+ '-'+self.author
name = models.CharField(max_length=120)
author = models.CharField(max_length=120)
genere = models.CharField(max_length=120)
year = models.CharField(max_length=120)
image = models.ImageField(upload_to='img/',null=True,blank=True)
forms.py
from django import forms
from .models import Book
SOME_CHOICES = (
('Science fiction','Science fiction'),
('Psychology','Psychology'),
('Philosophy','Philosophy'),
('Novel','Novel'),
('Poetry','Poetry'),
)
class BookForm(forms.Form):
name = forms.CharField()
author = forms.CharField()
genere = forms.CharField(label='Text',widget= forms .Select
(choices=SOME_CHOICES))
year = forms.CharField()
book_image = forms.ImageField()
views.py
from django.shortcuts import render,redirect
from books.forms import BookForm
from django import forms
from books.models import Book
from django.forms import ModelForm
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from django.shortcuts import render
from django.http import HttpResponse
from .models import Book
from django.views.generic import (ListView,CreateView,UpdateView,DeleteView,TemplateView)
from django.template import loader
from django.http import Http404
from django.views.generic.edit import CreateView
from django.core.urlresolvers import reverse
def Book_Create_View(request):
form=BookForm(request.POST,request.FILES)
if form.is_valid():
book_obj=models.Book()
book_obj.name = request.POST.get("name")
book_obj.author = request.POST.get("author")
book_obj.genere = request.POST.get("genere")
book_obj.year = request.POST.get("year")
book_obj.image = request.FILES
book_obj.save()
return redirect('books:index')
else:
form = BookForm()
template ='books/book_form.html'
return render(request,template,{'form':form})
book_form.html
{% extends 'books/base.html'%}
{%block content%}
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form}}
<div class="form-group">
<button type="submit" class="btn btn-sucess">Submit
</button>
</div>
</form>
{% endblock %}
urls.py
from django.conf.urls import url
from .import views
from books.views import ListView,BookCategoryListView,BookCreateView,UpdateView,Book_Create_View
from books import views
app_name='books'
urlpatterns = [
url(r'^$',views.IndexView.as_view(),name='index'),
# url(r'bookadd/$',views.book_create_view,name='add'),
# url(r'^add',views.book_create_view,name='book_add') ,
# url(r'^create/$',views.BookCreateView.as_view(),name='create') ,
url(r'^update/(?P<pk>\d+)/$',views.BookUpdateView.as_view(),name='update'),
url(r'^delete/(?P<pk>\d+)/$',views.BookDeleteView.as_view(),name='delete'),
url(r'^category/(?P<slug>\w+)$',views.BookCategoryListView.as_view()) ,
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(),name='detail'),
url(r'^create/$',views.Book_Create_View,name='create')
]
但是它返回错误
name 'models' is not defined
Ulls.py对我来说很好,因为我可以使用模型表单插入它,但是这里返回错误
如何解决此问题并使用表格插入值?
答案 0 :(得分:2)
您正在从模型中调用Book。您应该只调用Book,因为Book已经导入了。 将您的代码更改为此:
if form.is_valid():
book_obj=Book()
book_obj.name = request.POST.get("name")
book_obj.author = request.POST.get("author")
book_obj.genere = request.POST.get("genere")
book_obj.year = request.POST.get("year")
book_obj.save()
return redirect('books:index')