The project_list() view lists all the projects that already exist and gives you the option to add a new one. new_project() handles the creation of that new project then redirects to either the project() view or back to the project_list() including the form so it can display errors.
I keep thinking I should somehow be using the same render function. The reason is that for about a week I didn't realize that after a form.is_valid() == False
the page loaded properly but did not list the projects (I had forgotten to include the projects variable in the second render).
@login_required
def project_list(request):
projects = request.user.project_set.all()
return render(request, 'projects/project_list.html',
{'form': ProjectForm(),'projects': projects})
@login_required
def new_project(request):
form = ProjectForm()
if request.method == 'POST':
form = ProjectForm(data=request.POST)
if form.is_valid():
project = form.save(commit=False) #must specify user before commit
project.owner = request.user
project.save()
return redirect(project)
return render(request, 'projects/project_list.html',
{"form": form, 'projects': request.user.project_set.all()})
def project(request, project_id):
....
How do I keep this logic separate but use the same render call?
Also, hypothetically if I needed to render a third time this one with another additional variable, how should I account for that?