所以我一直在尝试将表单放在列表视图的同一索引页面上,并将数据发布到另一个视图来处理它。它适用于CreateView,但不适用于UpdateView。有谁知道如何使用基于类的视图来完成这项工作?
这是我的代码:
models.py
from django.db import models
class model1(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
views.py
from django.views.generic import *
from testapp.models import *
from django.core.urlresolvers import reverse_lazy
class list1(ListView):
model = model1
class create(CreateView):
model = model1
success_url = reverse_lazy('index')
class update(UpdateView):
model = model1
success_url = reverse_lazy('index')
view1 = list1()
view2 = create()
view3 = update()
class index(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
kwargs['view1_object_list'] = view1.get_query_set()
kwargs['cform'] = view2.get_form_class()
kwargs['uform'] = view3.get_form_class()
context = super(index, self).get_context_data(**kwargs)
return context
的index.html
List1
<br>
{% for object in view1_object_list %}
name: {{ object1.name }} description: {{ object1.description }}
Edit:
<form action="{% url 'update' pk=object.id %}" method="POST"> {% csrf_token %}
{{ uform.as_p }}
<input type="submit" value="Submit">
</form>
<br>
{% endfor %}
Create item for list1:
<br>
<form action="{% url 'create' %}" method="POST"> {% csrf_token %}
{{ cform.as_p }}
<input type="submit" value="Submit">
</form>
<br>
urls.py
from django.conf.urls import patterns, include, url
from testapp.views import *
urlpatterns = patterns('',
url(r'^index/$', index.as_view(), name='index'),
url(r'^create/$', create.as_view(), name='create'),
url(r'^update/(?P<pk>\d+)', update.as_view(), name='update'),
)