Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
how to get the instance of a newly created user in django function based view

i'm writing the logic in django where a newly created user would automatically get the free membership when they hit the signup button and right now, i know what to do but i don't know how to implement maybe becuase i don't have much experince with django. i defined an instance variable to store all the newly created information but i'm just stock and it keeps showing red underlined word in vsCode, Any help would be greatly appreciated

views.py

def register(request):
    reviews = Review.objects.filter(status='published')
    info = Announcements.objects.all()
    categories = Category.objects.all()
    if request.method == "POST":
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            obj = request.user
            get_membership = Membership.objects.get(membership_type='Free')
            # error is showing "instance" is not access using visual studio code
            instance = UserMembership.objects.create(user=obj, membership=get_membership)

            messages.success(request, f'Account Successfully created for {username}! You can Login In Now')
            return redirect('userauths:login')
    elif request.user.is_authenticated:
            return redirect('elements:home')
    else:
        form = UserRegisterForm()
    context = {
        'reviews': reviews,
        'form': form,
        'info': info,
        'categories': categories
    }
    return render(request, 'userauths/register.html', context)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

before saving register form that is where you need to get the user, it can be stored in variable and used to do something else later

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!