Search code examples
pythondjango

Tying data to a user's account


I am writing a signup form for my django site. And basically I want them to hit submit and create the account. I have written code that creates a license key when they hit it but I don't know how to tie it to their account. Any ideas?

from django.shortcuts import render, redirect
from django.contrib.auth import login
from .forms import SignupForm
from Functions.average_getter_shortened import baseline
from Functions.today_checker import today_getter
from Functions.license_key import license_getter
def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user = form.save()
            try:
                license_key = license_getter(access_key='access_key')
            except:
                license_key = 'Key'
            login(request, user)
            return render(request, r'registration\license_key.html', {'license_key' : license_key})
    else:
        form = SignupForm()
    return render(request, r'registration\signup.html', {'form': form})

I tried to implement user.save() after the except statement but then the website wouldnt redirect to the license_key page. I also tried my best to search up 'how to tie data to a user's account' but no luck.

I know this is quite a stupid question but I am new to django and couldn't find any resources on how to do this.


Solution

  • I just used the normal user model and saved the licensekey to it

    from django.shortcuts import render, redirect
    from django.contrib.auth import login
    from .forms import SignupForm
    from Functions.average_getter_shortened import baseline
    from Functions.today_checker import today_getter
    from Functions.license_key import license_getter
    from accounts.models import CustomUser
    def signup(request):
        if request.method == 'POST':
            form = SignupForm(request.POST)
            if form.is_valid():
                user = form.save()
                try:
                    license_key = license_getter(access_key='access_key')
                except:
                    license_key = 'Key'
                user.license_key = license_key
                user.save()
                login(request, user)
                return render(request, r'registration\license_key.html', {'license_key' : license_key})
        else:
            form = SignupForm()
        return render(request, r'registration\signup.html', {'form': form})