1
Class based views are a pretty good option, but there is no reason you can’t do something like;
def _render(request, form):
return render(request, 'projects/project_list.html',
{"form": form, 'projects': request.user.project_set.all()})
@login_required
def project_list(request):
projects = request.user.project_set.all()
form = ProjectForm()
return _render(request, form)
@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, form)
Source:stackexchange.com