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.