Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

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.

like image 850
Plasma Avatar asked Oct 13 '13 03:10

Plasma


2 Answers

With an CreateView its easy to make one without any complexity at all, dont believe the CBV haters ;)

class ListAndCreate(CreateView):
    model = YourModel
    template_name = "your-template.html"

    def get_context_data(self. **kwargs):
        context = super().get_context_data(**kwargs)
        context["objects"] = self.model.objects.all()
        return context

No loops, no conditionals means no problems testing it.

like image 141
krs Avatar answered Nov 01 '22 20:11

krs


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})
like image 37
Brandon Avatar answered Nov 01 '22 20:11

Brandon