Note that there are some explanatory texts on larger screens.

plurals
  1. POListView and CreateView in one template Django
    primarykey
    data
    text
    <p>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).</p> <p>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).</p> <p>Right now, this is what I am working with:</p> <p><strong>views.py:</strong></p> <pre><code>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) </code></pre> <p><strong>urls.py</strong></p> <pre><code>urlpatterns = patterns('', url(r'^projects/$', ProjectView.as_view(), name="projects"), ) </code></pre> <p><strong>models.py</strong></p> <pre><code>class Project(models.Model): name = models.CharField(max_length=200) def get_absolute_url(self): return reverse("projects") </code></pre> <p><strong>Code for the form</strong></p> <pre><code>&lt;form id="fileupload" method="post" action="." enctype="multipart/form-data"&gt; &lt;div class="row fileupload-buttonbar"&gt; &lt;div class="span7"&gt; &lt;span class="btn btn-primary fileinput-button"&gt; &lt;i class="icon-plus icon-white"&gt;&lt;/i&gt; &lt;span&gt;New Project&lt;/span&gt; &lt;input type="submit" name="Create"&gt; &lt;/span&gt; &lt;button type="button" class="btn btn-danger delete"&gt; &lt;i class="icon-trash icon-white"&gt;&lt;/i&gt; &lt;span&gt;Delete Project&lt;/span&gt; &lt;/button&gt; &lt;input type="checkbox" class="toggle"&gt; &lt;/div&gt; {{ form.as_p }} &lt;/div&gt; &lt;table class="table table-striped"&gt;&lt;tbody class="files"&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;/form&gt; </code></pre> <p>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:</p> <pre><code>NoReverseMatch at /upload/projects/ Reverse for 'projects' with arguments '()' and keyword arguments '{}' not found. </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload