-1

我收到的错误是

未定义全局名称“名称”...

subjects = Course...线。我想查询主题名称,以便可以在主题模板中使用它们

楷模

from django.db import models
from django.contrib.auth.models import User

class Course(models.Model):

name      = models.CharField(max_length = 100)
 stype      = models.CharField(max_length = 100)
 subject   = models.CharField(max_length = 100)
 location  = models.CharField(max_length = 100) 

class Teacher(models.Model):
 username      = models.ForeignKey(User,unique = True)
 address   = models.CharField(max_length = 200)
 city      = models.CharField(max_length = 50)   
 email_address = models.EmailField(max_length=75, unique = True)

class Student(models.Model):
 username      = models.ForeignKey(User,unique = True)
 address       = models.CharField(max_length = 200)
 city          = models.CharField(max_length = 50)   
 email_address = models.EmailField(max_length=75, unique = True)


class Attendance(models.Model):

 course    = models.ForeignKey(Course,unique = True)
 student   = models.ForeignKey(Student,unique = True)
 teacher   = models.ForeignKey(Teacher,unique = True)
 date      = models.DateField(unique = True)
 status    = models.BooleanField(unique = True)
 created_at= models.DateTimeField(auto_now_add = True)
 updated_at= models.DateTimeField(auto_now = True)

 def subjects(request,username):
    user      = User.objects.get(username=username)
    subjects  = Course.objects.get(name = name)
    variables = RequestContext(request,{
        'username': username,
        'subject' : subject
  })   
    return render_to_response('subjects.html', variables)
4

2 回答 2

1

subjects = Course.objects.get(name = name) 没有name定义。

您可以通过函数参数传递name类似的东西:username

def subjects(request,username, name):

或者代替name其他字符串,例如:

subjects = Course.objects.get(name ="course_name")

于 2012-06-04T11:38:37.537 回答
0

你进来username了,def subjects(request,username):但是..

subjects  = Course.objects.get(name = name)   ### name is not defined here...you have to get it from parameter just like username.
于 2012-06-04T11:31:58.663 回答