python - Attribute user.grade cannot be referenced in Django template -
i have code website i'm building.
in views.py
, have following code:
django.shortcuts import render django.http import httpresponse, httpresponseredirect django.conf.urls import url .models import userinfo, events django import forms .forms import registerform, loginform, orderform django.contrib.auth.models import user django.shortcuts import redirect django.views.decorators.csrf import csrf_exempt django.contrib.auth import authenticate, login, logout # create views here. def home(request): return render(request, 'student/index.html') @csrf_exempt def signin(request): print "login" if request.method == 'post': form = loginform(request.post) username = request.post['username'] password = request.post['password'] print "input username ", username try: if form.is_valid(): user = authenticate(username=username, password=password) if user not none: print "user not none" print user.username print user.email login(request,user) return redirect("/") else: print "login failed" raise forms.validationerror({'username':['invalid username/password']}) else: print form.errors except: raise else: print "hello" form = loginform() return render(request, 'student/login.html', {'form': form}) @csrf_exempt def signup(request): print "signup" if request.method == 'post': print "post signup" form = registerform(request.post) try: if form.is_valid(): print form.cleaned_data u = user.objects.create_user(form.cleaned_data['emailid'], form.cleaned_data['emailid'], form.cleaned_data['passwd1'] ) ui = userinfo() ui.user = u ui.class_of = form.cleaned_data['gradyear'] ui.grade = form.cleaned_data['grade'] ui.balance = 0 ui.save() user = authenticate(username=form.cleaned_data['emailid'], password=form.cleaned_data['passwd1']) login(request,user) print "after login in signup" return redirect("/") else: print "error" print form.errors except: raise print "error here" print form.errors pass #return render(request, 'student/register.html', {'form': form}) else: form = registerform() return render(request, 'student/register.html', {'form': form}) def forgotpassword(request): if request.method == 'post': form = loginform(request.post) if form.is_valid(): # process data in form.cleaned_data required # ... # redirect new url: print form.cleaned_data return httpresponseredirect('/thanks/') else: print "invalid" print form.errors else: form = loginform() return render(request, 'student/forgotpassword.html') def studentinfo(request): return render(request, 'student/studentinfo.html', {} ) def error(request): return render(request, 'student/loginerror.html', {} ) def site_logout(request): logout(request) return redirect("/") #return render(request, 'student/studentinfo.html', {} ) def order(request): if request.method == 'post': form = 0 return render(request, 'student/orderform.html') def dashboard(request): user = request.user return render(request, 'student/dashboard.html', {'user': user}) def blog(request): return render(request, 'student/blog.html')
and models.py
, userinfo
is:
from django.db import models django.contrib.auth.models import user class userinfo(models.model): user = models.foreignkey(user) class_of = models.integerfield() #username = user.username #fname = user.first_name #lname = user.last_name #email = user.email #staff = user.is_staff pub_date = models.datetimefield( auto_now=true) grade = models.integerfield() balance = models.decimalfield(max_digits=6, decimal_places=2) class events(models.model): name = models.charfield(max_length = 80) date = models.datetimefield() cost = models.decimalfield(max_digits = 6, decimal_places = 2) def __unicode__(self): return str(self.name) # create models here.
in html code using django template language, {{ user.username }}
, {{ user.email }}
show perfectly, why won't {{ user.grade }}
work same way?
grade
not attribute of django's user
model attribute of userinfo
, need need use like:
{{ user.user_infos.grade }}
where user_infos
related_name in model (it should onetoonefield
right?):
user = models.onetoonefield(user, related_name='user_infos')
Comments
Post a Comment