要将字段保存到数据库,请使用 aModelForm
或使用 new CreateView
,这更简单:
在你的views.py
:
from django.views.generic.edit import CreateView
from django.views.generic.base import TemplateView
from django.core.urlresolvers import reverse_lazy
from yourapp.models import SomeModel
class CreateSomeModel(CreateView):
model = SomeModel
template_name = 'some_template.html'
success_url = reverse_lazy('home')
class Home(TemplateView):
template_name = 'home.html'
在some_template.html
:
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit">
</form>
在home.html
:
<strong>This is the home page</strong>
在urls.py
:
from django.conf.urls import patterns, url
from yourapp.views import CreateSomeModel
from yourapp.views import Home
urlpatterns = patterns('',
url(r'^create/$', CreateSomeModel.as_view(), name='create'),
url(r'^$', Home.as_view(), name='home'),)