Tuesday, September 1, 2020

Adding to User new variable / money system in game

New to Django,

I want create a money system in my game that each user have a different coins

I tried add to User in views a variable 'balance'

My views.py:

  1. from monety.models import Gamer
  2. from django.contrib.auth.models import User 
  3. from .forms import RegisterForm

  4. def registration(response):
  5.     if response.method == "POST":
  6.         form = RegisterForm(response.POST)
  7.         if form.is_valid():
  8.             user = User.objects.all().filter(username=username)[0] 
  9.             gamer = Gamer(myuser=user)
  10.             form.save()
  11.             gamer.save()
  12.             return redirect('/garden')
  13.     else:
  14.         form = RegisterForm()

  15.     return render(response, "registration.html", {"form":form})
My models.py

  1. from django.db import models
  2. from django.contrib.auth.models import User 
  3.  
  4. class Gamer(models.Model):
  5.     myuser = models.OneToOneField(
  6.         User, 
  7.         on_delete=models.CASCADE
  8.     )
  9.     balance = models.IntegerField(default=100)
My forms.py

  1. from django.contrib.auth import login, authenticate
  2. from django.contrib.auth.forms import UserCreationForm
  3. from django import forms
  4. from django.contrib.auth.models import User

  5. class RegisterForm(UserCreationForm):
  6.     email = forms.EmailField()

  7.     class Meta:
  8.         model = User
  9.         fields = ["username", "email", "password1", "password2"]
I get "NameError: name 'username' is not defined" and I have no idea how to fix it to make it work

(sorry for my bad english)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7e414e72-0665-475b-bcdd-1eb66d905b83n%40googlegroups.com.

No comments:

Post a Comment