[ACCEPTED]-How do I pass the current user id as a hidden field in a Django form?-hidden
Accepted answer
It is much safer to get the current user 3 from the request after the form has been 2 submitted. You could rewrite the generic 1 view to something like this:
from django.shortcuts import redirect, render
def create_portfolio(request):
if request.method == 'POST':
form = PortfoliosCreateForm(request.POST)
if form.is_valid():
portfolio = form.save(commit=False)
portfolio.user = request.user # The logged-in user
portfolio.save()
return redirect('/')
else:
form = PortfoliosCreateForm()
return render(request, 'portfolios/create.html', {'form': form})
url(r'^portfolios/create/$', create_portfolio)
class PortfoliosCreateForm(ModelForm):
class Meta:
model = Portfolios;
exclude = ['user'] # Will be taken from the request
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.