class KissListView(ListView):
...
You are using ListView
which does not require form
and will not give you form
in the template context.
You may want to use CreateView
instead.
题
I've been using django for a couple of days now and I'm trying to create a small app to learn the whole stuff. I've read about the ModelForms and I wanted to use it in my app, but I can't get it to render in my template and I can't find the problem, so I was hoping you guys could help me out. Here's my code:
models.py
from django.db import models
class Kiss(models.Model):
latitude = models.FloatField()
longitude = models.FloatField()
person1 = models.CharField(max_length = 255)
person2 = models.CharField(max_length = 255)
cdate = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.person1
views.py
from django.views.generic.list import ListView
from project.models import Kiss
from project.forms import KissForm
from django.http import HttpResponseRedirect
class KissListView(ListView):
template_name = 'project/home.html'
model = Kiss
form = KissForm
urls.py (only the relevant part)
urlpatterns += patterns('',
url(r'^$', KissListView.as_view(), name='home'),
)
forms.py
from django import forms
from project.models import Kiss
class KissForm(forms.ModelForm):
class Meta:
model = Kiss
and the template
<form action="" method="POST">
{% csrf_token %}
{{form.as_p}}
<button>Send</button>
</form>
Thanks in advance for your help. J
解决方案
class KissListView(ListView):
...
You are using ListView
which does not require form
and will not give you form
in the template context.
You may want to use CreateView
instead.