[PYTHON] Pass login user information to view in Django

Note that I got stuck when passing the login user's id in the form

Premise

The version of Django is 1.10.

Implemented by referring to the following for login user sign-in and login. I'm using django.contrib.auth.models and django.contrib.auth.views that Django provides by default.

http://qiita.com/maueki/items/d28fd2a170d42e745376 http://nwpct1.hatenablog.com/entry/django-oauth-twitter-facebook-github

How to pass login user information to a view

Here, pass the id of the logged-in user as an example.

views.py


...
from django.contrib.auth.models import User
...

def test(request):
    login_user_id = request.user.id
    ...

The following is an example of saving the login user's ID at the same time as the information passed from the template by POST.

views.py


...
from django.contrib.auth.models import User
...
def test(request):
    if request.method == 'POST':
        form = TestForm(request.POST)
        if form.is_valid():
            test = form.save(commit=False)
            test.user_id = request.user.id
            test.save()
            return redirect('apps:templates')
    else:
            form = TestForm()

That's it, but it took a long time ...

Recommended Posts

Pass login user information to view in Django
[Django memo] I want to set the login user information in the form in advance
How to update user information when logging in to Django RemoteUserMiddleware
Output user information etc. to Django log
How to use bootstrap in Django generic class view
How to upload files in Django generic class view
Login to website in Python
Pass text to Django genericview
[Django] Pass the user instance authenticated by API to ModelSerializer
Errors related to memcached in django
How to reflect CSS in Django
Pass arguments to Task in discord.py
Information recording memo using session in Django
How to delete expired sessions in Django
How to get a namespaced view name from a URL (path_info) in Django
Ajax in Django (using generic class view)
How to do Server-Sent Events in Django
How to implement Scroll View in pythonista 1
How to convert DateTimeField format in Django
Implement a Custom User Model in Django
How to view images in Django's Admin
How to uniquely identify the source of access in the Django Generic Class View
Dynamically add fields to Form objects in Django
How to implement Rails helper-like functionality in Django
I want to pin Datetime.now in Django tests
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
How to pass settings to Item Pipeline in Scrapy
Display error message when login fails in Django
Specify the view URL in your Django template
I implemented Google's Speech to text in Django
Implement JWT login functionality in Django REST framework
How to create a Rest Api in Django
Add functionality to modify Django day shift information
How to hide user input in PySimple GUI
Django function-based view
How to use fixture in Django to populate sample data associated with a user model
Sometimes you want to access View information from Serializer with DRF (Django REST Framework)
Django class-based view
Forms in Django
Set the form DateField to type = date in Django
Implement the Django user extension and register the attached information
TemplateView patterns you'll want to learn first in Django
How to use Decorator in Django and how to make it
Allows you to select by name from Django User Name
Try to decipher the login data stored in Firefox
Do not pass self to ProcessPoolExecutor in the class
How to pass matplotlib backend settings in environment variables
How to reference static files in a Django project
[Django] How to redirect unlogged-in users to the login page
[Django] Field names, user registration, and login methods that can be used in the User model