V
V
Vadim kyklaed2017-06-19 22:19:12
Django
Vadim kyklaed, 2017-06-19 22:19:12

How to take value in view?

Hello everyone, what sections to read in the documentation?
there is a model, a form and a view.
set the foreign key in the model
user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True)
the user is authorized, how can I check it in the view? , I can have only one entry in the database with the form attached to a specific user. if the user has already filled out the form, then the form to fill out is not displayed to him, and if it is filled out, then there is a redirect to the profile page.
maybe someone will throw off links what to read specifically or just tell.

def user_data(request):
    if request.method == "POST":
        form = UserDataForm(request.POST)
        if form.is_valid():
            #уставливает связь сохраняемой формы с авторизованным юзером
            form.instance.user = request.user
            print(request.user)
            form.save()
            return redirect('/')
    else:
        form=UserDataForm()
    return render(request,'myoffice/userdata.html',{'form':form})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-06-19
@kyklaed

Remake the user profile in a human way, as the manual says
OneToOneField

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question