I have some code for a website that I'm building.
in my views.py, i have the following code:
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.conf.urls import url
from .models import UserInfo, Events
from django import forms
from .forms import RegisterForm, LoginForm, OrderForm
from django.contrib.auth.models import User
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate, login, logout
# Create your 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 is 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 the data in form.cleaned_data as required
# ...
# redirect to a 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 this is my models.py, where UserInfo is:
from django.db import models
from 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 your models here.
in my HTML code using the Django template language, {{ user.username }} and {{ user.email }} show up perfectly, but why won't {{ user.grade }} work the same way?
1 个答案:
答案 0 :(得分:2)
grade is not an attribute of Django's User model but is an attribute of UserInfo, you need need to use something like:
{{ user.user_infos.grade }}
Where user_infos is the related_name in your model (it should be OneToOneField right?):
user = models.OneToOneField(User, related_name='user_infos')