一个模板 Django 中的 ListView 和 CreateView [英] ListView and CreateView in one template Django
问题描述
我正在设计一个页面,人们可以在其中查看和创建某种类型的对象(这些对象是模型 Project 的实例).
据我所知,如果没有非常混乱的代码,我无法在一个视图中完成,所以我试图了解如何使用一个模板来显示两个视图(ProjectCreateView 和 ProjectListView).
现在,这就是我的工作:
views.py:
class ProjectCreateView(CreateView):模型 = 项目template_name = "文件上传/project_list.html"字段 = [名称"]def get_context_data(self, **kwargs):context = super(ProjectCreateView, self).get_context_data(**kwargs)返回上下文类 ProjectListView(ListView):模型 = 项目def get_context_data(self, **kwargs):context = super(ProjectListView, self).get_context_data(**kwargs)返回上下文类项目视图(视图):模型 = 项目def get(self, request, *args, **kwargs):视图 = ProjectListView.as_view()返回视图(请求,*args,**kwargs)def post(self, request, *args, **kwargs):视图 = ProjectCreateView.as_view()返回视图(请求,*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):返回反向(项目")
表单代码
{{ form.as_p }}
<table class="table table-striped"><tbody class="files"></tbody></table></表单>
但是,使用此配置,表单仅在按下按钮后显示名称"字段,输入名称后,我得到:
NoReverseMatch at/upload/projects/未找到带有参数()"和关键字参数{}"的项目"的反转.
正因如此,我猜测有一种比我正在做的更容易实现的方法.我很感激任何帮助.
一个整洁的基于函数的视图来列出和创建对象...
from django.shortcuts 导入渲染# 模型和表单导入def list_and_create(request):form = YourModelForm(request.POST or None)如果 request.method == 'POST' 和 form.is_valid():表单.save()# 注意这是在保存表单以获取新对象之后出现的对象 = YourModel.objects.all()return render(request, 'your-template.html', {'objects': objects, 'form': form})
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).
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")
Code for the form
<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})
这篇关于一个模板 Django 中的 ListView 和 CreateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!