models.py 我创建了一个Model,即Form_db,现在我想使用模板thanks.html
打印该信息from django.db import models
from django import forms
# Create your models here.
class Form_db(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField(max_length=50)
number = models.CharField(max_length=15)
any_query = models.TextField(blank=True,max_length=100)
address = models.TextField(max_length=100,blank=True)
def __str__(self): # __unicode__ on Python 2
return self.name
forms.py
from django import forms
from .models import Form_db
class Form_db_att(forms.ModelForm):
class Meta:
model = Form_db
fields= ['name','email','address','number','any_query']
views.py
from django.http import HttpResponseRedirect, HttpResponse, request
from django.shortcuts import redirect ,render
from .forms import Form_db_att
from django.utils import timezone
from .models import Form_db
# Create your views here.
def home(request):
if request.method == 'POST':
form = Form_db_att(request.POST)
if form.is_valid():
model_instance = form.save(commit=False)
model_instance.timestamp = timezone.now()
model_instance.save()
return HttpResponseRedirect('/home/test')
else:
form = Form_db_att()
return render (request,'mforma/name.html',{'form':form})
def thanks(request):
form = Form_db(request.POST)
return render(request,'mforma/thanks.html',{'form':form})
thanks.html form.id使用csrf标记返回值,并以列表格式显示字段值。
<form action="" method="post">
{{form.id}}
</form>
name.html
<form action="/home/thanks/" method="post" >
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="submit">
</form>
mformp / urls.py
from django.contrib import admin
from django.conf.urls import url , include
urlpatterns = [
url('admin/', admin.site.urls),
url('home/',include('mforma.urls')),
]
mformp / urls.py
from django.conf.urls import url
from . import views
urlpatterns =[
url(r'^$',views.home,name='home'),
url(r'^thanks/$',views.thanks,name='thanks')
]
答案 0 :(得分:0)
在form.save渲染thanks.html后,保存模型上下文。
model_instance.save()
return render(request, 'thanks.html', {'form': form})
thanks.html
在此感谢页面中,只需按{{ form }}
打印所有字段。