ListView和CreateView在一个模板Django中 [英] ListView and CreateView in one template Django
问题描述
我正在设计一个页面,人们可以在其中查看和创建某种类型的对象(对象是模型项目的实例)。
I'm designing a page in which people can view and create objects of a certain sort (the objects are instances of the model Project).
据我所知它,我不能在一个视图没有可怕的凌乱的代码,所以我想了解我如何使用一个模板来显示两个视图(ProjectCreateView和ProjectListView)。
As I understand it, I can't do it in one view without horribly messy code, so I am trying to understand how I can use one template to show two views (the ProjectCreateView and the ProjectListView).
现在,这正是我正在使用的:
Right now, this is what I am working with:
views.py:
class ProjectCreateView(CreateView):
model = Project
template_name = "fileupload/project_list.html"
fields = ["name"]
def get_context_data(self, **kwargs):
context = super(ProjectCreateView, self).get_context_data(**kwargs)
return context
class ProjectListView(ListView):
model = Project
def get_context_data(self, **kwargs):
context = super(ProjectListView, self).get_context_data(**kwargs)
return context
class ProjectView(View):
model = Project
def get(self, request, *args, **kwargs):
view = ProjectListView.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = ProjectCreateView.as_view()
return view(request, *args, **kwargs)
urls.py
urlpatterns = patterns('',
url(r'^projects/$', ProjectView.as_view(), name="projects"),
)
models.py
class Project(models.Model):
name = models.CharField(max_length=200)
def get_absolute_url(self):
return reverse("projects")
表单的代码
<form id="fileupload" method="post" action="." enctype="multipart/form-data">
<div class="row fileupload-buttonbar">
<div class="span7">
<span class="btn btn-primary fileinput-button">
<i class="icon-plus icon-white"></i>
<span>New Project</span>
<input type="submit" name="Create">
</span>
<button type="button" class="btn btn-danger delete">
<i class="icon-trash icon-white"></i>
<span>Delete Project</span>
</button>
<input type="checkbox" class="toggle">
</div>
{{ form.as_p }}
</div>
<table class="table table-striped"><tbody class="files"></tbody></table>
</form>
但是,使用此配置,该窗体仅在按钮按下后显示名称字段,并输入一个名字后,我得到这个:
However, with this configuration the form only shows the "name" field after the button has been pressed, and after entering in a name I get this:
NoReverseMatch at /upload/projects/
Reverse for 'projects' with arguments '()' and keyword arguments '{}' not found.
正因为如此,我猜想有一个比我更简单的实现这个方法这样做。我会感谢任何帮助。
Because of that, I'm guessing that there's a much easier way of implementing this than what I am doing. I'd appreciate any help.
推荐答案
一个不混乱的基于功能的视图列表和创建对象...
One un-messy function-based view to list and create objects...
from django.shortcuts import render
# model and form imports
def list_and_create(request):
form = YourModelForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
form.save()
# notice this comes after saving the form to pick up new objects
objects = YourModel.objects.all()
return render(request, 'your-template.html', {'objects': objects, 'form': form})
这篇关于ListView和CreateView在一个模板Django中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!