我有两个应用程序,“account”和“myapp”。我试图在视图中仅显示那些与 request.user 属于同一组织的教师对象。
帐户/models.pyfrom django.contrib.auth.models import User
class Organisation(models.Model):
name = models.CharField(max_length=100, unique=True)
is_active = models.BooleanField(default=True)
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
organisation = models.ForeignKey(Organisation, editable=False)
is_organisationadmin = models.BooleanField(default=False)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
Run Code Online (Sandbox Code Playgroud)
请注意此博客文章中的最后一行,它允许通过以下方式访问用户个人资料信息user.profile.organisation
from django.contrib.auth.models import User
class Teacher(models.Model):
user = models.OneToOneField(User, related_name='teacher')
Run Code Online (Sandbox Code Playgroud)
myapp/views.py
from myproject.account.models import Organisation, UserProfile
from myproject.myapp.models import Teacher
from django.contrib.auth.models import User
def homepage(request):
if request.user.is_authenticated():
teachers = Teacher.objects.filter(user.profile.organisation == request.user.profile.organisation, user__is_active = True)
Run Code Online (Sandbox Code Playgroud)
我收到“/homepage/ 处的 NameError,未定义全局名称“用户””。我认为这是因为我没有正确访问每个 Teacher 对象的 …
我有兴趣设置一个环境来使用 pyOpenGL 编写粒子模拟器。我已经安装了 pyOpenGL。这是测试代码,只是为了确保 opengl 正常工作。
from OpenGL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
window = 0
width, height = 500,400
def draw():
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glutDwapBuffers()
#initialization
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width,height)
glutInitWindowPosition(0,0)
window = glutCreateWindow(b"noobtute")
glutDisplayFunc(draw)
glutIdleFunc(draw)
glutMainLoop()
Run Code Online (Sandbox Code Playgroud)
但是,当我从命令运行脚本时,出现以下错误:
NameError: name 'glClear' is not defined
GLUT Display callback <function draw at 0x0000000002F8CB70> with (),{} failed:
returning None name 'glClear' is not defined
Run Code Online (Sandbox Code Playgroud)
我尝试重新安装 pyOpengl 但没有成功。
我在 Windows 8.1 上运行 Python …
我似乎在某种程度上搞砸了最基本的东西.我有:
def function(a, b, c):
return 'hi'
Run Code Online (Sandbox Code Playgroud)
print(function(a,b,c))导致NameError每个变量.
这是什么原因?
我正在尝试使用以下方法将脚本的路径存储到变量中:
os.path.abspath(os.path.dirname(__file__))
Run Code Online (Sandbox Code Playgroud)
但是,它不断返回name '__file__' is not defined错误。
here = os.path.dirname(os.path.abspath(__file__))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
Run Code Online (Sandbox Code Playgroud) 尝试训练一个 Robust CNN 模型,其定义如下:
from keras.datasets import cifar10
from keras.utils import np_utils
from keras import metrics
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, LSTM, merge
from keras.layers import BatchNormalization
from keras import metrics
from keras.losses import categorical_crossentropy
from keras.optimizers import SGD
import pickle
import matplotlib.pyplot as plt
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras import layers
from keras.callbacks import EarlyStopping
def Robust_CNN():
model = Sequential()
model.add(Conv2D(256, (3, 3), activation='relu', padding='same', init='glorot_uniform', input_shape=(2,128,1)))
model.add(BatchNormalization()) …Run Code Online (Sandbox Code Playgroud) 我试图将我的线程代码转换为多处理代码.但它给了我错误
Name Error: global name 'multiprocessing' is not defined
Run Code Online (Sandbox Code Playgroud)
安装了多处理,我将其导入
from multiprocessing import *
Run Code Online (Sandbox Code Playgroud) 我正在使用spyder并编写了以下类:
class Ray:
def __init__(self, r, p, k):
if r.shape == (3,):
self.r = r
if p.shape == (3,):
self.p = p
if k.shape == (3,):
self.k = k
r = array(range(3))
p = array(range(3))
k = array(range(3))
Run Code Online (Sandbox Code Playgroud)
它存储在/ home/user/workspace/spyder/project中,控制台工作目录就是那个.在控制台中,我可以运行一个数组(范围(3)),它返回一个值为0,1,2的数组.但是在做的时候
import ray
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ray.py", line 8, in <module>
class Ray:
File "ray.py", line 20, in ray
r = array(range(3));
NameError: name 'array' is not defined
Run Code Online (Sandbox Code Playgroud)
编辑:
默认情况下spyder有以下行为,不太明白为什么array()默认工作我认为它只是numpy的一部分.
import …Run Code Online (Sandbox Code Playgroud) 如果我的某个地方有某个地方,我会提前道歉.
我是Python编程的新手,并试图从Jan Erik Solem编写的" Programming Computer Vision with Python "中计算一个例子.
以下是示例的代码(保存在名为"harris.py"的文件中):
https://github.com/jesolem/PCV/blob/master/pcv_book/harris.py
(请注意,我的代码在第70行结束)
在对此代码进行了很好的解释之后,在书中说明:"尝试运行以下命令:"
im = array(Image.open(’empire.jpg’).convert(’L’))
harrisim = harris.compute_harris_response(im)
filtered_coords = harris.get_harris_points(harrisim,6)
harris.plot_harris_points(im, filtered_coords)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题有两个:
在我运行它的任何文件中,显示以下错误:
harrisim = harris.compute_harris_response(im)
NameError: name 'harris' is not defined
我不明白为什么会出现这个错误,因为'harris'应该调用python脚本harris.py.还是我错了?
我正在运行Windows 7,只安装了Python 2.7.4,我得到的只是SyntaxError:'s.例如,当我键入:
>>>ls
Run Code Online (Sandbox Code Playgroud)
我明白了
Traceback (most recent call last):
file "<stdin>", line 1, in <module>
NameError: name 'ls' is not defined.
Run Code Online (Sandbox Code Playgroud)
我是Python新手,不知道问题是什么.数学计算工作以及代码似乎都有效.这只是命令,如cd,ls,sudo apt-get update(和upgrade).任何指导将不胜感激.
我正在尝试为cms网站建立一个管理员后端.
这是我的应用程序的结构
??? app.rb
??? Gemfile
??? models
? ??? models.rb
??? routes
? ??? routes.rb
??? views
??? categories.erb
??? # ... other view files
Run Code Online (Sandbox Code Playgroud)
app.rb
require 'sinatra'
require 'data_mapper'
require 'dm-core'
require 'dm-migrations'
require 'digest'
enable :sessions
DataMapper.setup(:default, 'mysql://username:password@localhost/database')
require './models/models.rb'
require './routes/routes.rb'
DataMapper.finalize
Run Code Online (Sandbox Code Playgroud)
models.rb
class Category
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :posts
end
# other model definitons
Run Code Online (Sandbox Code Playgroud)
我在routes.rb中定义了categories一条路线
...
get '/categories' do
@categories = Category.all
erb :categories
end …Run Code Online (Sandbox Code Playgroud) nameerror ×10
python ×8
command-line ×1
django ×1
keras ×1
one-to-one ×1
pyopengl ×1
python-3.x ×1
ruby ×1
sinatra ×1
spyder ×1
user-profile ×1
windows-8.1 ×1